script_editor_plugin.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  1. /*************************************************************************/
  2. /* script_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-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 "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/script_editor_debugger.h"
  34. #include "global_config.h"
  35. #include "io/resource_loader.h"
  36. #include "io/resource_saver.h"
  37. #include "os/file_access.h"
  38. #include "os/input.h"
  39. #include "os/keyboard.h"
  40. #include "os/keyboard.h"
  41. #include "os/os.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("request_help_search", PropertyInfo(Variant::STRING, "topic")));
  47. ADD_SIGNAL(MethodInfo("request_open_script_at_line", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::INT, "line")));
  48. ADD_SIGNAL(MethodInfo("request_save_history"));
  49. ADD_SIGNAL(MethodInfo("go_to_help", PropertyInfo(Variant::STRING, "what")));
  50. }
  51. static bool _can_open_in_editor(Script *p_script) {
  52. String path = p_script->get_path();
  53. if (path.find("::") != -1) {
  54. //refuse handling this if it can't be edited
  55. bool valid = false;
  56. for (int i = 0; i < EditorNode::get_singleton()->get_editor_data().get_edited_scene_count(); i++) {
  57. if (path.begins_with(EditorNode::get_singleton()->get_editor_data().get_scene_path(i))) {
  58. valid = true;
  59. break;
  60. }
  61. }
  62. return valid;
  63. }
  64. return true;
  65. }
  66. class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
  67. struct Cache {
  68. uint64_t time_loaded;
  69. RES cache;
  70. };
  71. Map<String, Cache> cached;
  72. public:
  73. uint64_t max_time_cache;
  74. int max_cache_size;
  75. void cleanup() {
  76. List<Map<String, Cache>::Element *> to_clean;
  77. Map<String, Cache>::Element *I = cached.front();
  78. while (I) {
  79. if ((OS::get_singleton()->get_ticks_msec() - I->get().time_loaded) > max_time_cache) {
  80. to_clean.push_back(I);
  81. }
  82. I = I->next();
  83. }
  84. while (to_clean.front()) {
  85. cached.erase(to_clean.front()->get());
  86. to_clean.pop_front();
  87. }
  88. }
  89. RES get_cached_resource(const String &p_path) {
  90. Map<String, Cache>::Element *E = cached.find(p_path);
  91. if (!E) {
  92. Cache c;
  93. c.cache = ResourceLoader::load(p_path);
  94. E = cached.insert(p_path, c);
  95. }
  96. E->get().time_loaded = OS::get_singleton()->get_ticks_msec();
  97. if (cached.size() > max_cache_size) {
  98. uint64_t older;
  99. Map<String, Cache>::Element *O = cached.front();
  100. older = O->get().time_loaded;
  101. Map<String, Cache>::Element *I = O;
  102. while (I) {
  103. if (I->get().time_loaded < older) {
  104. older = I->get().time_loaded;
  105. O = I;
  106. }
  107. I = I->next();
  108. }
  109. if (O != E) { //should never heppane..
  110. cached.erase(O);
  111. }
  112. }
  113. return E->get().cache;
  114. }
  115. EditorScriptCodeCompletionCache() {
  116. max_cache_size = 128;
  117. max_time_cache = 5 * 60 * 1000; //minutes, five
  118. }
  119. };
  120. #define SORT_SCRIPT_LIST
  121. void ScriptEditorQuickOpen::popup(const Vector<String> &p_functions, bool p_dontclear) {
  122. popup_centered_ratio(0.6);
  123. if (p_dontclear)
  124. search_box->select_all();
  125. else
  126. search_box->clear();
  127. search_box->grab_focus();
  128. functions = p_functions;
  129. _update_search();
  130. }
  131. void ScriptEditorQuickOpen::_text_changed(const String &p_newtext) {
  132. _update_search();
  133. }
  134. void ScriptEditorQuickOpen::_sbox_input(const InputEvent &p_ie) {
  135. if (p_ie.type == InputEvent::KEY && (p_ie.key.scancode == KEY_UP ||
  136. p_ie.key.scancode == KEY_DOWN ||
  137. p_ie.key.scancode == KEY_PAGEUP ||
  138. p_ie.key.scancode == KEY_PAGEDOWN)) {
  139. search_options->call("_gui_input", p_ie);
  140. search_box->accept_event();
  141. }
  142. }
  143. void ScriptEditorQuickOpen::_update_search() {
  144. search_options->clear();
  145. TreeItem *root = search_options->create_item();
  146. for (int i = 0; i < functions.size(); i++) {
  147. String file = functions[i];
  148. if ((search_box->get_text() == "" || file.findn(search_box->get_text()) != -1)) {
  149. TreeItem *ti = search_options->create_item(root);
  150. ti->set_text(0, file);
  151. if (root->get_children() == ti)
  152. ti->select(0);
  153. }
  154. }
  155. get_ok()->set_disabled(root->get_children() == NULL);
  156. }
  157. void ScriptEditorQuickOpen::_confirmed() {
  158. TreeItem *ti = search_options->get_selected();
  159. if (!ti)
  160. return;
  161. int line = ti->get_text(0).get_slice(":", 1).to_int();
  162. emit_signal("goto_line", line - 1);
  163. hide();
  164. }
  165. void ScriptEditorQuickOpen::_notification(int p_what) {
  166. if (p_what == NOTIFICATION_ENTER_TREE) {
  167. connect("confirmed", this, "_confirmed");
  168. }
  169. }
  170. void ScriptEditorQuickOpen::_bind_methods() {
  171. ClassDB::bind_method(D_METHOD("_text_changed"), &ScriptEditorQuickOpen::_text_changed);
  172. ClassDB::bind_method(D_METHOD("_confirmed"), &ScriptEditorQuickOpen::_confirmed);
  173. ClassDB::bind_method(D_METHOD("_sbox_input"), &ScriptEditorQuickOpen::_sbox_input);
  174. ADD_SIGNAL(MethodInfo("goto_line", PropertyInfo(Variant::INT, "line")));
  175. }
  176. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  177. VBoxContainer *vbc = memnew(VBoxContainer);
  178. add_child(vbc);
  179. //set_child_rect(vbc);
  180. search_box = memnew(LineEdit);
  181. vbc->add_margin_child(TTR("Search:"), search_box);
  182. search_box->connect("text_changed", this, "_text_changed");
  183. search_box->connect("gui_input", this, "_sbox_input");
  184. search_options = memnew(Tree);
  185. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  186. get_ok()->set_text(TTR("Open"));
  187. get_ok()->set_disabled(true);
  188. register_text_enter(search_box);
  189. set_hide_on_ok(false);
  190. search_options->connect("item_activated", this, "_confirmed");
  191. search_options->set_hide_root(true);
  192. }
  193. /////////////////////////////////
  194. ScriptEditor *ScriptEditor::script_editor = NULL;
  195. /*** SCRIPT EDITOR ******/
  196. String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *_se) {
  197. //ScriptEditorBase *se=_se->cast_to<ScriptEditorBase>();
  198. String val = debugger->get_var_value(p_text);
  199. if (val != String()) {
  200. return p_text + ": " + val;
  201. } else {
  202. return String();
  203. }
  204. }
  205. void ScriptEditor::_breaked(bool p_breaked, bool p_can_debug) {
  206. if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  207. return;
  208. }
  209. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  210. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug));
  211. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked);
  212. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked);
  213. for (int i = 0; i < tab_container->get_child_count(); i++) {
  214. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  215. if (!se) {
  216. continue;
  217. }
  218. se->set_debugger_active(p_breaked);
  219. }
  220. }
  221. void ScriptEditor::_show_debugger(bool p_show) {
  222. //debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  223. }
  224. void ScriptEditor::_script_created(Ref<Script> p_script) {
  225. editor->push_item(p_script.operator->());
  226. }
  227. void ScriptEditor::_goto_script_line2(int p_line) {
  228. int selected = tab_container->get_current_tab();
  229. if (selected < 0 || selected >= tab_container->get_child_count())
  230. return;
  231. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  232. if (!current)
  233. return;
  234. current->goto_line(p_line);
  235. }
  236. void ScriptEditor::_goto_script_line(REF p_script, int p_line) {
  237. editor->push_item(p_script.ptr());
  238. int selected = tab_container->get_current_tab();
  239. if (selected < 0 || selected >= tab_container->get_child_count())
  240. return;
  241. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  242. if (!current)
  243. return;
  244. current->goto_line(p_line, true);
  245. }
  246. void ScriptEditor::_update_history_arrows() {
  247. script_back->set_disabled(history_pos <= 0);
  248. script_forward->set_disabled(history_pos >= history.size() - 1);
  249. }
  250. void ScriptEditor::_save_history() {
  251. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  252. Node *n = tab_container->get_current_tab_control();
  253. if (n->cast_to<ScriptEditorBase>()) {
  254. history[history_pos].state = n->cast_to<ScriptEditorBase>()->get_edit_state();
  255. }
  256. if (n->cast_to<EditorHelp>()) {
  257. history[history_pos].state = n->cast_to<EditorHelp>()->get_scroll();
  258. }
  259. }
  260. history.resize(history_pos + 1);
  261. ScriptHistory sh;
  262. sh.control = tab_container->get_current_tab_control();
  263. sh.state = Variant();
  264. history.push_back(sh);
  265. history_pos++;
  266. _update_history_arrows();
  267. }
  268. void ScriptEditor::_go_to_tab(int p_idx) {
  269. Node *cn = tab_container->get_child(p_idx);
  270. if (!cn)
  271. return;
  272. Control *c = cn->cast_to<Control>();
  273. if (!c)
  274. return;
  275. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  276. Node *n = tab_container->get_current_tab_control();
  277. if (n->cast_to<ScriptEditorBase>()) {
  278. history[history_pos].state = n->cast_to<ScriptEditorBase>()->get_edit_state();
  279. }
  280. if (n->cast_to<EditorHelp>()) {
  281. history[history_pos].state = n->cast_to<EditorHelp>()->get_scroll();
  282. }
  283. }
  284. history.resize(history_pos + 1);
  285. ScriptHistory sh;
  286. sh.control = c;
  287. sh.state = Variant();
  288. history.push_back(sh);
  289. history_pos++;
  290. tab_container->set_current_tab(p_idx);
  291. c = tab_container->get_current_tab_control();
  292. if (c->cast_to<ScriptEditorBase>()) {
  293. script_name_label->set_text(c->cast_to<ScriptEditorBase>()->get_name());
  294. script_icon->set_texture(c->cast_to<ScriptEditorBase>()->get_icon());
  295. if (is_visible_in_tree())
  296. c->cast_to<ScriptEditorBase>()->ensure_focus();
  297. }
  298. if (c->cast_to<EditorHelp>()) {
  299. script_name_label->set_text(c->cast_to<EditorHelp>()->get_class());
  300. script_icon->set_texture(get_icon("Help", "EditorIcons"));
  301. if (is_visible_in_tree())
  302. c->cast_to<EditorHelp>()->set_focused();
  303. }
  304. c->set_meta("__editor_pass", ++edit_pass);
  305. _update_history_arrows();
  306. _update_script_colors();
  307. _update_selected_editor_menu();
  308. }
  309. void ScriptEditor::_close_tab(int p_idx, bool p_save) {
  310. int selected = p_idx;
  311. if (selected < 0 || selected >= tab_container->get_child_count())
  312. return;
  313. Node *tselected = tab_container->get_child(selected);
  314. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  315. if (current) {
  316. if (p_save) {
  317. apply_scripts();
  318. }
  319. if (current->get_edit_menu()) {
  320. memdelete(current->get_edit_menu());
  321. }
  322. }
  323. //remove from history
  324. history.resize(history_pos + 1);
  325. for (int i = 0; i < history.size(); i++) {
  326. if (history[i].control == tselected) {
  327. history.remove(i);
  328. i--;
  329. history_pos--;
  330. }
  331. }
  332. if (history_pos >= history.size()) {
  333. history_pos = history.size() - 1;
  334. }
  335. int idx = tab_container->get_current_tab();
  336. memdelete(tselected);
  337. if (idx >= tab_container->get_child_count())
  338. idx = tab_container->get_child_count() - 1;
  339. if (idx >= 0) {
  340. if (history_pos >= 0) {
  341. idx = history[history_pos].control->get_index();
  342. }
  343. tab_container->set_current_tab(idx);
  344. //script_list->select(idx);
  345. }
  346. _update_history_arrows();
  347. _update_script_names();
  348. _save_layout();
  349. }
  350. void ScriptEditor::_close_current_tab() {
  351. _close_tab(tab_container->get_current_tab());
  352. }
  353. void ScriptEditor::_close_discard_current_tab(const String &p_str) {
  354. _close_tab(tab_container->get_current_tab(), false);
  355. erase_tab_confirm->hide();
  356. }
  357. void ScriptEditor::_close_docs_tab() {
  358. int child_count = tab_container->get_child_count();
  359. for (int i = child_count - 1; i >= 0; i--) {
  360. EditorHelp *se = tab_container->get_child(i)->cast_to<EditorHelp>();
  361. if (se) {
  362. _close_tab(i);
  363. }
  364. }
  365. }
  366. void ScriptEditor::_close_all_tabs() {
  367. int child_count = tab_container->get_child_count();
  368. for (int i = child_count - 1; i >= 0; i--) {
  369. tab_container->set_current_tab(i);
  370. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  371. if (se) {
  372. // Maybe there are unsaved changes
  373. if (se->is_unsaved()) {
  374. _ask_close_current_unsaved_tab(se);
  375. continue;
  376. }
  377. }
  378. _close_current_tab();
  379. }
  380. }
  381. void ScriptEditor::_ask_close_current_unsaved_tab(ScriptEditorBase *current) {
  382. erase_tab_confirm->set_text("Close and save changes?\n\"" + current->get_name() + "\"");
  383. erase_tab_confirm->popup_centered_minsize();
  384. }
  385. void ScriptEditor::_resave_scripts(const String &p_str) {
  386. apply_scripts();
  387. for (int i = 0; i < tab_container->get_child_count(); i++) {
  388. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  389. if (!se)
  390. continue;
  391. Ref<Script> script = se->get_edited_script();
  392. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)
  393. continue; //internal script, who cares
  394. if (trim_trailing_whitespace_on_save) {
  395. se->trim_trailing_whitespace();
  396. }
  397. editor->save_resource(script);
  398. se->tag_saved_version();
  399. }
  400. disk_changed->hide();
  401. }
  402. void ScriptEditor::_reload_scripts() {
  403. for (int i = 0; i < tab_container->get_child_count(); i++) {
  404. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  405. if (!se) {
  406. continue;
  407. }
  408. Ref<Script> script = se->get_edited_script();
  409. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  410. continue; //internal script, who cares
  411. }
  412. uint64_t last_date = script->get_last_modified_time();
  413. uint64_t date = FileAccess::get_modified_time(script->get_path());
  414. //printf("last date: %lli vs date: %lli\n",last_date,date);
  415. if (last_date == date) {
  416. continue;
  417. }
  418. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), true);
  419. ERR_CONTINUE(!rel_script.is_valid());
  420. script->set_source_code(rel_script->get_source_code());
  421. script->set_last_modified_time(rel_script->get_last_modified_time());
  422. script->reload();
  423. se->reload_text();
  424. }
  425. disk_changed->hide();
  426. _update_script_names();
  427. }
  428. void ScriptEditor::_res_saved_callback(const Ref<Resource> &p_res) {
  429. for (int i = 0; i < tab_container->get_child_count(); i++) {
  430. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  431. if (!se) {
  432. continue;
  433. }
  434. Ref<Script> script = se->get_edited_script();
  435. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  436. continue; //internal script, who cares
  437. }
  438. if (script == p_res) {
  439. se->tag_saved_version();
  440. }
  441. }
  442. _update_script_names();
  443. if (!pending_auto_reload && auto_reload_running_scripts) {
  444. call_deferred("_live_auto_reload_running_scripts");
  445. pending_auto_reload = true;
  446. }
  447. }
  448. void ScriptEditor::_live_auto_reload_running_scripts() {
  449. pending_auto_reload = false;
  450. debugger->reload_scripts();
  451. }
  452. bool ScriptEditor::_test_script_times_on_disk(Ref<Script> p_for_script) {
  453. disk_changed_list->clear();
  454. TreeItem *r = disk_changed_list->create_item();
  455. disk_changed_list->set_hide_root(true);
  456. bool need_ask = false;
  457. bool need_reload = false;
  458. bool use_autoreload = bool(EDITOR_DEF("text_editor/files/auto_reload_scripts_on_external_change", false));
  459. for (int i = 0; i < tab_container->get_child_count(); i++) {
  460. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  461. if (se) {
  462. Ref<Script> script = se->get_edited_script();
  463. if (p_for_script.is_valid() && p_for_script != script)
  464. continue;
  465. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)
  466. continue; //internal script, who cares
  467. uint64_t last_date = script->get_last_modified_time();
  468. uint64_t date = FileAccess::get_modified_time(script->get_path());
  469. //printf("last date: %lli vs date: %lli\n",last_date,date);
  470. if (last_date != date) {
  471. TreeItem *ti = disk_changed_list->create_item(r);
  472. ti->set_text(0, script->get_path().get_file());
  473. if (!use_autoreload || se->is_unsaved()) {
  474. need_ask = true;
  475. }
  476. need_reload = true;
  477. //r->set_metadata(0,);
  478. }
  479. }
  480. }
  481. if (need_reload) {
  482. if (!need_ask) {
  483. script_editor->_reload_scripts();
  484. need_reload = false;
  485. } else {
  486. disk_changed->call_deferred("popup_centered_ratio", 0.5);
  487. }
  488. }
  489. return need_reload;
  490. }
  491. void ScriptEditor::_file_dialog_action(String p_file) {
  492. switch (file_dialog_option) {
  493. case FILE_SAVE_THEME_AS: {
  494. if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
  495. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  496. }
  497. } break;
  498. case FILE_IMPORT_THEME: {
  499. if (!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
  500. editor->show_warning(TTR("Error importing theme"), TTR("Error importing"));
  501. }
  502. } break;
  503. }
  504. file_dialog_option = -1;
  505. }
  506. void ScriptEditor::_menu_option(int p_option) {
  507. switch (p_option) {
  508. case FILE_NEW: {
  509. script_create_dialog->config("Node", ".gd");
  510. script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
  511. } break;
  512. case FILE_OPEN: {
  513. editor->open_resource("Script");
  514. return;
  515. } break;
  516. case FILE_SAVE_ALL: {
  517. if (_test_script_times_on_disk())
  518. return;
  519. save_all_scripts();
  520. } break;
  521. case FILE_IMPORT_THEME: {
  522. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  523. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  524. file_dialog_option = FILE_IMPORT_THEME;
  525. file_dialog->clear_filters();
  526. file_dialog->add_filter("*.tet");
  527. file_dialog->popup_centered_ratio();
  528. file_dialog->set_title(TTR("Import Theme"));
  529. } break;
  530. case FILE_RELOAD_THEME: {
  531. EditorSettings::get_singleton()->load_text_editor_theme();
  532. } break;
  533. case FILE_SAVE_THEME: {
  534. if (!EditorSettings::get_singleton()->save_text_editor_theme()) {
  535. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  536. }
  537. } break;
  538. case FILE_SAVE_THEME_AS: {
  539. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  540. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  541. file_dialog_option = FILE_SAVE_THEME_AS;
  542. file_dialog->clear_filters();
  543. file_dialog->add_filter("*.tet");
  544. file_dialog->set_current_path(EditorSettings::get_singleton()->get_settings_path() + "/text_editor_themes/" + EditorSettings::get_singleton()->get("text_editor/theme/color_theme"));
  545. file_dialog->popup_centered_ratio();
  546. file_dialog->set_title(TTR("Save Theme As.."));
  547. } break;
  548. case SEARCH_HELP: {
  549. help_search_dialog->popup();
  550. } break;
  551. case SEARCH_CLASSES: {
  552. String current;
  553. if (tab_container->get_tab_count() > 0) {
  554. EditorHelp *eh = tab_container->get_child(tab_container->get_current_tab())->cast_to<EditorHelp>();
  555. if (eh) {
  556. current = eh->get_class();
  557. }
  558. }
  559. help_index->popup();
  560. if (current != "") {
  561. help_index->call_deferred("select_class", current);
  562. }
  563. } break;
  564. case SEARCH_WEBSITE: {
  565. OS::get_singleton()->shell_open("http://docs.godotengine.org/");
  566. } break;
  567. case WINDOW_NEXT: {
  568. _history_forward();
  569. } break;
  570. case WINDOW_PREV: {
  571. _history_back();
  572. } break;
  573. case DEBUG_SHOW: {
  574. if (debugger) {
  575. bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW));
  576. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  577. if (visible)
  578. debugger->hide();
  579. else
  580. debugger->show();
  581. }
  582. } break;
  583. case DEBUG_SHOW_KEEP_OPEN: {
  584. bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN));
  585. if (debugger)
  586. debugger->set_hide_on_stop(visible);
  587. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN), !visible);
  588. } break;
  589. }
  590. int selected = tab_container->get_current_tab();
  591. if (selected < 0 || selected >= tab_container->get_child_count())
  592. return;
  593. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  594. if (current) {
  595. switch (p_option) {
  596. case FILE_NEW: {
  597. script_create_dialog->config("Node", ".gd");
  598. script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
  599. } break;
  600. case FILE_SAVE: {
  601. if (_test_script_times_on_disk())
  602. return;
  603. if (trim_trailing_whitespace_on_save)
  604. current->trim_trailing_whitespace();
  605. editor->save_resource(current->get_edited_script());
  606. } break;
  607. case FILE_SAVE_AS: {
  608. current->trim_trailing_whitespace();
  609. editor->push_item(current->get_edited_script()->cast_to<Object>());
  610. editor->save_resource_as(current->get_edited_script());
  611. } break;
  612. case FILE_TOOL_RELOAD:
  613. case FILE_TOOL_RELOAD_SOFT: {
  614. current->reload(p_option == FILE_TOOL_RELOAD_SOFT);
  615. } break;
  616. case FILE_CLOSE: {
  617. if (current->is_unsaved()) {
  618. _ask_close_current_unsaved_tab(current);
  619. } else {
  620. _close_current_tab();
  621. }
  622. } break;
  623. case CLOSE_DOCS: {
  624. _close_docs_tab();
  625. } break;
  626. case CLOSE_ALL: {
  627. _close_all_tabs();
  628. } break;
  629. case DEBUG_NEXT: {
  630. if (debugger)
  631. debugger->debug_next();
  632. } break;
  633. case DEBUG_STEP: {
  634. if (debugger)
  635. debugger->debug_step();
  636. } break;
  637. case DEBUG_BREAK: {
  638. if (debugger)
  639. debugger->debug_break();
  640. } break;
  641. case DEBUG_CONTINUE: {
  642. if (debugger)
  643. debugger->debug_continue();
  644. } break;
  645. case WINDOW_MOVE_LEFT: {
  646. if (tab_container->get_current_tab() > 0) {
  647. tab_container->call_deferred("set_current_tab", tab_container->get_current_tab() - 1);
  648. script_list->call_deferred("select", tab_container->get_current_tab() - 1);
  649. tab_container->move_child(current, tab_container->get_current_tab() - 1);
  650. _update_script_names();
  651. }
  652. } break;
  653. case WINDOW_MOVE_RIGHT: {
  654. if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
  655. tab_container->call_deferred("set_current_tab", tab_container->get_current_tab() + 1);
  656. script_list->call_deferred("select", tab_container->get_current_tab() + 1);
  657. tab_container->move_child(current, tab_container->get_current_tab() + 1);
  658. _update_script_names();
  659. }
  660. } break;
  661. default: {
  662. if (p_option >= WINDOW_SELECT_BASE) {
  663. tab_container->set_current_tab(p_option - WINDOW_SELECT_BASE);
  664. script_list->select(p_option - WINDOW_SELECT_BASE);
  665. }
  666. }
  667. }
  668. } else {
  669. EditorHelp *help = tab_container->get_current_tab_control()->cast_to<EditorHelp>();
  670. if (help) {
  671. switch (p_option) {
  672. case HELP_SEARCH_FIND: {
  673. help->popup_search();
  674. } break;
  675. case HELP_SEARCH_FIND_NEXT: {
  676. help->search_again();
  677. } break;
  678. case FILE_CLOSE: {
  679. _close_current_tab();
  680. } break;
  681. case CLOSE_DOCS: {
  682. _close_docs_tab();
  683. } break;
  684. case CLOSE_ALL: {
  685. _close_all_tabs();
  686. } break;
  687. }
  688. }
  689. }
  690. }
  691. void ScriptEditor::_tab_changed(int p_which) {
  692. ensure_select_current();
  693. }
  694. void ScriptEditor::_notification(int p_what) {
  695. if (p_what == NOTIFICATION_ENTER_TREE) {
  696. editor->connect("play_pressed", this, "_editor_play");
  697. editor->connect("pause_pressed", this, "_editor_pause");
  698. editor->connect("stop_pressed", this, "_editor_stop");
  699. editor->connect("script_add_function_request", this, "_add_callback");
  700. editor->connect("resource_saved", this, "_res_saved_callback");
  701. script_list->connect("item_selected", this, "_script_selected");
  702. script_split->connect("dragged", this, "_script_split_dragged");
  703. autosave_timer->connect("timeout", this, "_autosave_scripts");
  704. {
  705. float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs");
  706. if (autosave_time > 0) {
  707. autosave_timer->set_wait_time(autosave_time);
  708. autosave_timer->start();
  709. } else {
  710. autosave_timer->stop();
  711. }
  712. }
  713. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  714. help_search->set_icon(get_icon("Help", "EditorIcons"));
  715. site_search->set_icon(get_icon("Godot", "EditorIcons"));
  716. class_search->set_icon(get_icon("ClassList", "EditorIcons"));
  717. script_forward->set_icon(get_icon("Forward", "EditorIcons"));
  718. script_back->set_icon(get_icon("Back", "EditorIcons"));
  719. }
  720. if (p_what == NOTIFICATION_READY) {
  721. get_tree()->connect("tree_changed", this, "_tree_changed");
  722. editor->connect("request_help", this, "_request_help");
  723. }
  724. if (p_what == NOTIFICATION_EXIT_TREE) {
  725. editor->disconnect("play_pressed", this, "_editor_play");
  726. editor->disconnect("pause_pressed", this, "_editor_pause");
  727. editor->disconnect("stop_pressed", this, "_editor_stop");
  728. }
  729. if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  730. _test_script_times_on_disk();
  731. _update_modified_scripts_for_external_editor();
  732. }
  733. if (p_what == NOTIFICATION_PROCESS) {
  734. }
  735. }
  736. bool ScriptEditor::can_take_away_focus() const {
  737. int selected = tab_container->get_current_tab();
  738. if (selected < 0 || selected >= tab_container->get_child_count())
  739. return true;
  740. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  741. if (!current)
  742. return true;
  743. return current->can_lose_focus_on_node_selection();
  744. }
  745. void ScriptEditor::close_builtin_scripts_from_scene(const String &p_scene) {
  746. for (int i = 0; i < tab_container->get_child_count(); i++) {
  747. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  748. if (se) {
  749. Ref<Script> script = se->get_edited_script();
  750. if (!script.is_valid())
  751. continue;
  752. if (script->get_path().find("::") != -1 && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
  753. _close_tab(i);
  754. i--;
  755. }
  756. }
  757. }
  758. }
  759. void ScriptEditor::edited_scene_changed() {
  760. _update_modified_scripts_for_external_editor();
  761. }
  762. static const Node *_find_node_with_script(const Node *p_node, const RefPtr &p_script) {
  763. if (p_node->get_script() == p_script)
  764. return p_node;
  765. for (int i = 0; i < p_node->get_child_count(); i++) {
  766. const Node *result = _find_node_with_script(p_node->get_child(i), p_script);
  767. if (result)
  768. return result;
  769. }
  770. return NULL;
  771. }
  772. Dictionary ScriptEditor::get_state() const {
  773. //apply_scripts();
  774. Dictionary state;
  775. #if 0
  776. Array paths;
  777. int open=-1;
  778. for(int i=0;i<tab_container->get_child_count();i++) {
  779. ScriptTextEditor *se = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  780. if (!se)
  781. continue;
  782. Ref<Script> script = se->get_edited_script();
  783. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  784. paths.push_back(script->get_path());
  785. } else {
  786. const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
  787. if (owner)
  788. paths.push_back(owner->get_path());
  789. }
  790. if (i==tab_container->get_current_tab())
  791. open=i;
  792. }
  793. if (paths.size())
  794. state["sources"]=paths;
  795. if (open!=-1)
  796. state["current"]=open;
  797. #endif
  798. return state;
  799. }
  800. void ScriptEditor::set_state(const Dictionary &p_state) {
  801. #if 0
  802. print_line("attempt set state: "+String(Variant(p_state)));
  803. if (!p_state.has("sources"))
  804. return; //bleh
  805. Array sources = p_state["sources"];
  806. for(int i=0;i<sources.size();i++) {
  807. Variant source=sources[i];
  808. Ref<Script> script;
  809. if (source.get_type()==Variant::NODE_PATH) {
  810. Node *owner=get_tree()->get_root()->get_node(source);
  811. if (!owner)
  812. continue;
  813. script = owner->get_script();
  814. } else if (source.get_type()==Variant::STRING) {
  815. script = ResourceLoader::load(source,"Script");
  816. }
  817. if (script.is_null()) //ah well..
  818. continue;
  819. editor->call("_resource_selected",script);
  820. }
  821. if (p_state.has("current")) {
  822. tab_container->set_current_tab(p_state["current"]);
  823. }
  824. #endif
  825. }
  826. void ScriptEditor::clear() {
  827. #if 0
  828. List<ScriptTextEditor*> stes;
  829. for(int i=0;i<tab_container->get_child_count();i++) {
  830. ScriptTextEditor *se = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  831. if (!se)
  832. continue;
  833. stes.push_back(se);
  834. }
  835. while(stes.size()) {
  836. memdelete(stes.front()->get());
  837. stes.pop_front();
  838. }
  839. int idx = tab_container->get_current_tab();
  840. if (idx>=tab_container->get_child_count())
  841. idx=tab_container->get_child_count()-1;
  842. if (idx>=0) {
  843. tab_container->set_current_tab(idx);
  844. script_list->select( script_list->find_metadata(idx) );
  845. }
  846. #endif
  847. }
  848. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  849. for (int i = 0; i < tab_container->get_child_count(); i++) {
  850. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  851. if (!se)
  852. continue;
  853. List<int> bpoints;
  854. se->get_breakpoints(&bpoints);
  855. Ref<Script> script = se->get_edited_script();
  856. String base = script->get_path();
  857. ERR_CONTINUE(base.begins_with("local://") || base == "");
  858. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  859. p_breakpoints->push_back(base + ":" + itos(E->get() + 1));
  860. }
  861. }
  862. }
  863. void ScriptEditor::ensure_focus_current() {
  864. if (!is_inside_tree())
  865. return;
  866. int cidx = tab_container->get_current_tab();
  867. if (cidx < 0 || cidx >= tab_container->get_tab_count())
  868. ;
  869. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  870. if (!c)
  871. return;
  872. ScriptEditorBase *se = c->cast_to<ScriptEditorBase>();
  873. if (!se)
  874. return;
  875. se->ensure_focus();
  876. }
  877. void ScriptEditor::_script_selected(int p_idx) {
  878. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  879. _go_to_tab(script_list->get_item_metadata(p_idx));
  880. grab_focus_block = false;
  881. }
  882. void ScriptEditor::ensure_select_current() {
  883. if (tab_container->get_child_count() && tab_container->get_current_tab() >= 0) {
  884. Node *current = tab_container->get_child(tab_container->get_current_tab());
  885. ScriptEditorBase *se = current->cast_to<ScriptEditorBase>();
  886. if (se) {
  887. Ref<Script> script = se->get_edited_script();
  888. if (!grab_focus_block && is_visible_in_tree())
  889. se->ensure_focus();
  890. //edit_menu->show();
  891. //search_menu->show();
  892. }
  893. EditorHelp *eh = current->cast_to<EditorHelp>();
  894. if (eh) {
  895. //edit_menu->hide();
  896. //search_menu->hide();
  897. //script_search_menu->show();
  898. }
  899. }
  900. _update_selected_editor_menu();
  901. }
  902. void ScriptEditor::_find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used) {
  903. if (p_current != p_base && p_current->get_owner() != p_base)
  904. return;
  905. if (p_current->get_script_instance()) {
  906. Ref<Script> scr = p_current->get_script();
  907. if (scr.is_valid())
  908. used.insert(scr);
  909. }
  910. for (int i = 0; i < p_current->get_child_count(); i++) {
  911. _find_scripts(p_base, p_current->get_child(i), used);
  912. }
  913. }
  914. struct _ScriptEditorItemData {
  915. String name;
  916. String sort_key;
  917. Ref<Texture> icon;
  918. int index;
  919. String tooltip;
  920. bool used;
  921. int category;
  922. bool operator<(const _ScriptEditorItemData &id) const {
  923. return category == id.category ? sort_key < id.sort_key : category < id.category;
  924. }
  925. };
  926. void ScriptEditor::_update_script_colors() {
  927. bool script_temperature_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_enabled");
  928. bool highlight_current = EditorSettings::get_singleton()->get("text_editor/open_scripts/highlight_current_script");
  929. int hist_size = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_history_size");
  930. Color hot_color = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_hot_color");
  931. Color cold_color = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_cold_color");
  932. for (int i = 0; i < script_list->get_item_count(); i++) {
  933. int c = script_list->get_item_metadata(i);
  934. Node *n = tab_container->get_child(c);
  935. if (!n)
  936. continue;
  937. script_list->set_item_custom_bg_color(i, Color(0, 0, 0, 0));
  938. bool current = tab_container->get_current_tab() == c;
  939. if (current && highlight_current) {
  940. script_list->set_item_custom_bg_color(i, EditorSettings::get_singleton()->get("text_editor/open_scripts/current_script_background_color"));
  941. } else if (script_temperature_enabled) {
  942. if (!n->has_meta("__editor_pass")) {
  943. continue;
  944. }
  945. int pass = n->get_meta("__editor_pass");
  946. int h = edit_pass - pass;
  947. if (h > hist_size) {
  948. continue;
  949. }
  950. int non_zero_hist_size = (hist_size == 0) ? 1 : hist_size;
  951. float v = Math::ease((edit_pass - pass) / float(non_zero_hist_size), 0.4);
  952. script_list->set_item_custom_bg_color(i, hot_color.linear_interpolate(cold_color, v));
  953. }
  954. }
  955. }
  956. void ScriptEditor::_update_script_names() {
  957. if (restoring_layout)
  958. return;
  959. waiting_update_names = false;
  960. Set<Ref<Script> > used;
  961. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  962. if (edited) {
  963. _find_scripts(edited, edited, used);
  964. }
  965. script_list->clear();
  966. bool split_script_help = EditorSettings::get_singleton()->get("text_editor/open_scripts/group_help_pages");
  967. ScriptSortBy sort_by = (ScriptSortBy)(int)EditorSettings::get_singleton()->get("text_editor/open_scripts/sort_scripts_by");
  968. ScriptListName display_as = (ScriptListName)(int)EditorSettings::get_singleton()->get("text_editor/open_scripts/list_script_names_as");
  969. Vector<_ScriptEditorItemData> sedata;
  970. for (int i = 0; i < tab_container->get_child_count(); i++) {
  971. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  972. if (se) {
  973. String name = se->get_name();
  974. Ref<Texture> icon = se->get_icon();
  975. String path = se->get_edited_script()->get_path();
  976. _ScriptEditorItemData sd;
  977. sd.icon = icon;
  978. sd.name = name;
  979. sd.tooltip = path;
  980. sd.index = i;
  981. sd.used = used.has(se->get_edited_script());
  982. sd.category = 0;
  983. switch (sort_by) {
  984. case SORT_BY_NAME: {
  985. sd.sort_key = name.to_lower();
  986. } break;
  987. case SORT_BY_PATH: {
  988. sd.sort_key = path;
  989. } break;
  990. }
  991. switch (display_as) {
  992. case DISPLAY_NAME: {
  993. sd.name = name;
  994. } break;
  995. case DISPLAY_DIR_AND_NAME: {
  996. if (!path.get_base_dir().get_file().empty()) {
  997. sd.name = path.get_base_dir().get_file() + "/" + name;
  998. } else {
  999. sd.name = name;
  1000. }
  1001. } break;
  1002. case DISPLAY_FULL_PATH: {
  1003. sd.name = path;
  1004. } break;
  1005. }
  1006. sedata.push_back(sd);
  1007. }
  1008. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1009. if (eh) {
  1010. String name = eh->get_class();
  1011. Ref<Texture> icon = get_icon("Help", "EditorIcons");
  1012. String tooltip = name + " Class Reference";
  1013. _ScriptEditorItemData sd;
  1014. sd.icon = icon;
  1015. sd.name = name;
  1016. sd.sort_key = name;
  1017. sd.tooltip = tooltip;
  1018. sd.index = i;
  1019. sd.used = false;
  1020. sd.category = split_script_help ? 1 : 0;
  1021. sedata.push_back(sd);
  1022. }
  1023. }
  1024. sedata.sort();
  1025. for (int i = 0; i < sedata.size(); i++) {
  1026. script_list->add_item(sedata[i].name, sedata[i].icon);
  1027. int index = script_list->get_item_count() - 1;
  1028. script_list->set_item_tooltip(index, sedata[i].tooltip);
  1029. script_list->set_item_metadata(index, sedata[i].index);
  1030. if (sedata[i].used) {
  1031. script_list->set_item_custom_bg_color(index, Color(88 / 255.0, 88 / 255.0, 60 / 255.0));
  1032. }
  1033. if (tab_container->get_current_tab() == sedata[i].index) {
  1034. script_list->select(index);
  1035. script_name_label->set_text(sedata[i].name);
  1036. script_icon->set_texture(sedata[i].icon);
  1037. }
  1038. }
  1039. _update_script_colors();
  1040. }
  1041. void ScriptEditor::edit(const Ref<Script> &p_script, bool p_grab_focus) {
  1042. if (p_script.is_null())
  1043. return;
  1044. // refuse to open built-in if scene is not loaded
  1045. // see if already has it
  1046. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change");
  1047. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  1048. String path = EditorSettings::get_singleton()->get("text_editor/external/exec_path");
  1049. String flags = EditorSettings::get_singleton()->get("text_editor/external/exec_flags");
  1050. List<String> args;
  1051. flags = flags.strip_edges();
  1052. if (flags != String()) {
  1053. Vector<String> flagss = flags.split(" ", false);
  1054. for (int i = 0; i < flagss.size(); i++)
  1055. args.push_back(flagss[i]);
  1056. }
  1057. args.push_back(GlobalConfig::get_singleton()->globalize_path(p_script->get_path()));
  1058. Error err = OS::get_singleton()->execute(path, args, false);
  1059. if (err == OK)
  1060. return;
  1061. WARN_PRINT("Couldn't open external text editor, using internal");
  1062. }
  1063. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1064. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1065. if (!se)
  1066. continue;
  1067. if (se->get_edited_script() == p_script) {
  1068. if (open_dominant || !EditorNode::get_singleton()->is_changing_scene()) {
  1069. if (tab_container->get_current_tab() != i) {
  1070. _go_to_tab(i);
  1071. script_list->select(script_list->find_metadata(i));
  1072. }
  1073. if (is_visible_in_tree())
  1074. se->ensure_focus();
  1075. }
  1076. return;
  1077. }
  1078. }
  1079. // doesn't have it, make a new one
  1080. ScriptEditorBase *se;
  1081. for (int i = script_editor_func_count - 1; i >= 0; i--) {
  1082. se = script_editor_funcs[i](p_script);
  1083. if (se)
  1084. break;
  1085. }
  1086. ERR_FAIL_COND(!se);
  1087. tab_container->add_child(se);
  1088. se->set_edited_script(p_script);
  1089. se->set_tooltip_request_func("_get_debug_tooltip", this);
  1090. if (se->get_edit_menu()) {
  1091. se->get_edit_menu()->hide();
  1092. menu_hb->add_child(se->get_edit_menu());
  1093. menu_hb->move_child(se->get_edit_menu(), 1);
  1094. }
  1095. if (p_grab_focus) {
  1096. _go_to_tab(tab_container->get_tab_count() - 1);
  1097. }
  1098. _update_script_names();
  1099. _save_layout();
  1100. se->connect("name_changed", this, "_update_script_names");
  1101. se->connect("request_help_search", this, "_help_search");
  1102. se->connect("request_open_script_at_line", this, "_goto_script_line");
  1103. se->connect("go_to_help", this, "_help_class_goto");
  1104. se->connect("request_save_history", this, "_save_history");
  1105. //test for modification, maybe the script was not edited but was loaded
  1106. _test_script_times_on_disk(p_script);
  1107. _update_modified_scripts_for_external_editor(p_script);
  1108. }
  1109. void ScriptEditor::save_all_scripts() {
  1110. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1111. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1112. if (!se)
  1113. continue;
  1114. if (!se->is_unsaved())
  1115. continue;
  1116. if (trim_trailing_whitespace_on_save) {
  1117. se->trim_trailing_whitespace();
  1118. }
  1119. Ref<Script> script = se->get_edited_script();
  1120. if (script.is_valid())
  1121. se->apply_code();
  1122. if (script->get_path() != "" && script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  1123. //external script, save it
  1124. editor->save_resource(script);
  1125. //ResourceSaver::save(script->get_path(),script);
  1126. }
  1127. }
  1128. _update_script_names();
  1129. }
  1130. void ScriptEditor::apply_scripts() const {
  1131. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1132. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1133. if (!se)
  1134. continue;
  1135. se->apply_code();
  1136. }
  1137. }
  1138. void ScriptEditor::_editor_play() {
  1139. debugger->start();
  1140. debug_menu->get_popup()->grab_focus();
  1141. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1142. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1143. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false);
  1144. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1145. //debugger_gui->start_listening(Globals::get_singleton()->get("debug/debug_port"));
  1146. }
  1147. void ScriptEditor::_editor_pause() {
  1148. }
  1149. void ScriptEditor::_editor_stop() {
  1150. debugger->stop();
  1151. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1152. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1153. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
  1154. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1155. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1156. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1157. if (!se) {
  1158. continue;
  1159. }
  1160. se->set_debugger_active(false);
  1161. }
  1162. }
  1163. void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const PoolStringArray &p_args) {
  1164. //print_line("add callback! hohoho"); kinda sad to remove this
  1165. ERR_FAIL_COND(!p_obj);
  1166. Ref<Script> script = p_obj->get_script();
  1167. ERR_FAIL_COND(!script.is_valid());
  1168. editor->push_item(script.ptr());
  1169. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1170. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1171. if (!se)
  1172. continue;
  1173. if (se->get_edited_script() != script)
  1174. continue;
  1175. se->add_callback(p_function, p_args);
  1176. _go_to_tab(i);
  1177. script_list->select(script_list->find_metadata(i));
  1178. break;
  1179. }
  1180. }
  1181. void ScriptEditor::_save_layout() {
  1182. if (restoring_layout) {
  1183. return;
  1184. }
  1185. editor->save_layout();
  1186. }
  1187. void ScriptEditor::_editor_settings_changed() {
  1188. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/files/trim_trailing_whitespace_on_save");
  1189. float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs");
  1190. if (autosave_time > 0) {
  1191. autosave_timer->set_wait_time(autosave_time);
  1192. autosave_timer->start();
  1193. } else {
  1194. autosave_timer->stop();
  1195. }
  1196. if (current_theme == "") {
  1197. current_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  1198. } else if (current_theme != EditorSettings::get_singleton()->get("text_editor/theme/color_theme")) {
  1199. current_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  1200. EditorSettings::get_singleton()->load_text_editor_theme();
  1201. }
  1202. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1203. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1204. if (!se)
  1205. continue;
  1206. se->update_settings();
  1207. }
  1208. _update_script_colors();
  1209. _update_script_names();
  1210. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/files/auto_reload_and_parse_scripts_on_save", true));
  1211. }
  1212. void ScriptEditor::_autosave_scripts() {
  1213. save_all_scripts();
  1214. }
  1215. void ScriptEditor::_tree_changed() {
  1216. if (waiting_update_names)
  1217. return;
  1218. waiting_update_names = true;
  1219. call_deferred("_update_script_names");
  1220. }
  1221. void ScriptEditor::_script_split_dragged(float) {
  1222. _save_layout();
  1223. }
  1224. void ScriptEditor::_unhandled_input(const InputEvent &p_event) {
  1225. if (p_event.key.pressed || !is_visible_in_tree()) return;
  1226. if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
  1227. int next_tab = script_list->get_current() + 1;
  1228. next_tab %= script_list->get_item_count();
  1229. _go_to_tab(script_list->get_item_metadata(next_tab));
  1230. _update_script_names();
  1231. }
  1232. if (ED_IS_SHORTCUT("script_editor/prev_script", p_event)) {
  1233. int next_tab = script_list->get_current() - 1;
  1234. next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1;
  1235. _go_to_tab(script_list->get_item_metadata(next_tab));
  1236. _update_script_names();
  1237. }
  1238. }
  1239. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  1240. if (!bool(EDITOR_DEF("text_editor/files/restore_scripts_on_load", true))) {
  1241. return;
  1242. }
  1243. if (!p_layout->has_section_key("ScriptEditor", "open_scripts") && !p_layout->has_section_key("ScriptEditor", "open_help"))
  1244. return;
  1245. Array scripts = p_layout->get_value("ScriptEditor", "open_scripts");
  1246. Array helps;
  1247. if (p_layout->has_section_key("ScriptEditor", "open_help"))
  1248. helps = p_layout->get_value("ScriptEditor", "open_help");
  1249. restoring_layout = true;
  1250. for (int i = 0; i < scripts.size(); i++) {
  1251. String path = scripts[i];
  1252. if (!FileAccess::exists(path))
  1253. continue;
  1254. Ref<Script> scr = ResourceLoader::load(path);
  1255. if (scr.is_valid()) {
  1256. edit(scr);
  1257. }
  1258. }
  1259. for (int i = 0; i < helps.size(); i++) {
  1260. String path = helps[i];
  1261. _help_class_open(path);
  1262. }
  1263. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1264. tab_container->get_child(i)->set_meta("__editor_pass", Variant());
  1265. }
  1266. if (p_layout->has_section_key("ScriptEditor", "split_offset")) {
  1267. script_split->set_split_offset(p_layout->get_value("ScriptEditor", "split_offset"));
  1268. }
  1269. restoring_layout = false;
  1270. _update_script_names();
  1271. }
  1272. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  1273. Array scripts;
  1274. Array helps;
  1275. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1276. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1277. if (se) {
  1278. String path = se->get_edited_script()->get_path();
  1279. if (!path.is_resource_file())
  1280. continue;
  1281. scripts.push_back(path);
  1282. }
  1283. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1284. if (eh) {
  1285. helps.push_back(eh->get_class());
  1286. }
  1287. }
  1288. p_layout->set_value("ScriptEditor", "open_scripts", scripts);
  1289. p_layout->set_value("ScriptEditor", "open_help", helps);
  1290. p_layout->set_value("ScriptEditor", "split_offset", script_split->get_split_offset());
  1291. }
  1292. void ScriptEditor::_help_class_open(const String &p_class) {
  1293. if (p_class == "")
  1294. return;
  1295. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1296. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1297. if (eh && eh->get_class() == p_class) {
  1298. _go_to_tab(i);
  1299. _update_script_names();
  1300. return;
  1301. }
  1302. }
  1303. EditorHelp *eh = memnew(EditorHelp);
  1304. eh->set_name(p_class);
  1305. tab_container->add_child(eh);
  1306. _go_to_tab(tab_container->get_tab_count() - 1);
  1307. eh->go_to_class(p_class, 0);
  1308. eh->connect("go_to_help", this, "_help_class_goto");
  1309. _update_script_names();
  1310. _save_layout();
  1311. }
  1312. void ScriptEditor::_help_class_goto(const String &p_desc) {
  1313. String cname = p_desc.get_slice(":", 1);
  1314. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1315. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1316. if (eh && eh->get_class() == cname) {
  1317. _go_to_tab(i);
  1318. eh->go_to_help(p_desc);
  1319. _update_script_names();
  1320. return;
  1321. }
  1322. }
  1323. EditorHelp *eh = memnew(EditorHelp);
  1324. eh->set_name(cname);
  1325. tab_container->add_child(eh);
  1326. _go_to_tab(tab_container->get_tab_count() - 1);
  1327. eh->go_to_help(p_desc);
  1328. eh->connect("go_to_help", this, "_help_class_goto");
  1329. _update_script_names();
  1330. _save_layout();
  1331. }
  1332. void ScriptEditor::_update_selected_editor_menu() {
  1333. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1334. bool current = tab_container->get_current_tab() == i;
  1335. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1336. if (se && se->get_edit_menu()) {
  1337. if (current)
  1338. se->get_edit_menu()->show();
  1339. else
  1340. se->get_edit_menu()->hide();
  1341. }
  1342. }
  1343. EditorHelp *eh = tab_container->get_current_tab_control()->cast_to<EditorHelp>();
  1344. if (eh) {
  1345. script_search_menu->show();
  1346. } else {
  1347. script_search_menu->hide();
  1348. }
  1349. }
  1350. void ScriptEditor::_update_history_pos(int p_new_pos) {
  1351. Node *n = tab_container->get_current_tab_control();
  1352. if (n->cast_to<ScriptEditorBase>()) {
  1353. history[history_pos].state = n->cast_to<ScriptEditorBase>()->get_edit_state();
  1354. }
  1355. if (n->cast_to<EditorHelp>()) {
  1356. history[history_pos].state = n->cast_to<EditorHelp>()->get_scroll();
  1357. }
  1358. history_pos = p_new_pos;
  1359. tab_container->set_current_tab(history[history_pos].control->get_index());
  1360. n = history[history_pos].control;
  1361. if (n->cast_to<ScriptEditorBase>()) {
  1362. n->cast_to<ScriptEditorBase>()->set_edit_state(history[history_pos].state);
  1363. n->cast_to<ScriptEditorBase>()->ensure_focus();
  1364. }
  1365. if (n->cast_to<EditorHelp>()) {
  1366. n->cast_to<EditorHelp>()->set_scroll(history[history_pos].state);
  1367. n->cast_to<EditorHelp>()->set_focused();
  1368. }
  1369. n->set_meta("__editor_pass", ++edit_pass);
  1370. _update_script_names();
  1371. _update_history_arrows();
  1372. _update_selected_editor_menu();
  1373. }
  1374. void ScriptEditor::_history_forward() {
  1375. if (history_pos < history.size() - 1) {
  1376. _update_history_pos(history_pos + 1);
  1377. }
  1378. }
  1379. void ScriptEditor::_history_back() {
  1380. if (history_pos > 0) {
  1381. _update_history_pos(history_pos - 1);
  1382. }
  1383. }
  1384. void ScriptEditor::set_scene_root_script(Ref<Script> p_script) {
  1385. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change");
  1386. if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")))
  1387. return;
  1388. if (open_dominant && p_script.is_valid() && _can_open_in_editor(p_script.ptr())) {
  1389. edit(p_script);
  1390. }
  1391. }
  1392. bool ScriptEditor::script_go_to_method(Ref<Script> p_script, const String &p_method) {
  1393. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1394. ScriptEditorBase *current = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1395. if (current && current->get_edited_script() == p_script) {
  1396. if (current->goto_method(p_method)) {
  1397. edit(p_script);
  1398. return true;
  1399. }
  1400. break;
  1401. }
  1402. }
  1403. return false;
  1404. }
  1405. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  1406. auto_reload_running_scripts = p_enabled;
  1407. }
  1408. void ScriptEditor::_help_search(String p_text) {
  1409. help_search_dialog->popup(p_text);
  1410. }
  1411. void ScriptEditor::_open_script_request(const String &p_path) {
  1412. Ref<Script> script = ResourceLoader::load(p_path);
  1413. if (script.is_valid()) {
  1414. script_editor->edit(script, false);
  1415. }
  1416. }
  1417. int ScriptEditor::script_editor_func_count = 0;
  1418. CreateScriptEditorFunc ScriptEditor::script_editor_funcs[ScriptEditor::SCRIPT_EDITOR_FUNC_MAX];
  1419. void ScriptEditor::register_create_script_editor_function(CreateScriptEditorFunc p_func) {
  1420. ERR_FAIL_COND(script_editor_func_count == SCRIPT_EDITOR_FUNC_MAX);
  1421. script_editor_funcs[script_editor_func_count++] = p_func;
  1422. }
  1423. void ScriptEditor::_bind_methods() {
  1424. ClassDB::bind_method("_file_dialog_action", &ScriptEditor::_file_dialog_action);
  1425. ClassDB::bind_method("_tab_changed", &ScriptEditor::_tab_changed);
  1426. ClassDB::bind_method("_menu_option", &ScriptEditor::_menu_option);
  1427. ClassDB::bind_method("_close_current_tab", &ScriptEditor::_close_current_tab);
  1428. ClassDB::bind_method("_close_discard_current_tab", &ScriptEditor::_close_discard_current_tab);
  1429. ClassDB::bind_method("_close_docs_tab", &ScriptEditor::_close_docs_tab);
  1430. ClassDB::bind_method("_close_all_tabs", &ScriptEditor::_close_all_tabs);
  1431. ClassDB::bind_method("_editor_play", &ScriptEditor::_editor_play);
  1432. ClassDB::bind_method("_editor_pause", &ScriptEditor::_editor_pause);
  1433. ClassDB::bind_method("_editor_stop", &ScriptEditor::_editor_stop);
  1434. ClassDB::bind_method("_add_callback", &ScriptEditor::_add_callback);
  1435. ClassDB::bind_method("_reload_scripts", &ScriptEditor::_reload_scripts);
  1436. ClassDB::bind_method("_resave_scripts", &ScriptEditor::_resave_scripts);
  1437. ClassDB::bind_method("_res_saved_callback", &ScriptEditor::_res_saved_callback);
  1438. ClassDB::bind_method("_goto_script_line", &ScriptEditor::_goto_script_line);
  1439. ClassDB::bind_method("_goto_script_line2", &ScriptEditor::_goto_script_line2);
  1440. ClassDB::bind_method("_help_search", &ScriptEditor::_help_search);
  1441. ClassDB::bind_method("_save_history", &ScriptEditor::_save_history);
  1442. ClassDB::bind_method("_breaked", &ScriptEditor::_breaked);
  1443. ClassDB::bind_method("_show_debugger", &ScriptEditor::_show_debugger);
  1444. ClassDB::bind_method("_get_debug_tooltip", &ScriptEditor::_get_debug_tooltip);
  1445. ClassDB::bind_method("_autosave_scripts", &ScriptEditor::_autosave_scripts);
  1446. ClassDB::bind_method("_editor_settings_changed", &ScriptEditor::_editor_settings_changed);
  1447. ClassDB::bind_method("_update_script_names", &ScriptEditor::_update_script_names);
  1448. ClassDB::bind_method("_tree_changed", &ScriptEditor::_tree_changed);
  1449. ClassDB::bind_method("_script_selected", &ScriptEditor::_script_selected);
  1450. ClassDB::bind_method("_script_created", &ScriptEditor::_script_created);
  1451. ClassDB::bind_method("_script_split_dragged", &ScriptEditor::_script_split_dragged);
  1452. ClassDB::bind_method("_help_class_open", &ScriptEditor::_help_class_open);
  1453. ClassDB::bind_method("_help_class_goto", &ScriptEditor::_help_class_goto);
  1454. ClassDB::bind_method("_request_help", &ScriptEditor::_help_class_open);
  1455. ClassDB::bind_method("_history_forward", &ScriptEditor::_history_forward);
  1456. ClassDB::bind_method("_history_back", &ScriptEditor::_history_back);
  1457. ClassDB::bind_method("_live_auto_reload_running_scripts", &ScriptEditor::_live_auto_reload_running_scripts);
  1458. ClassDB::bind_method("_unhandled_input", &ScriptEditor::_unhandled_input);
  1459. }
  1460. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  1461. current_theme = "";
  1462. completion_cache = memnew(EditorScriptCodeCompletionCache);
  1463. restoring_layout = false;
  1464. waiting_update_names = false;
  1465. pending_auto_reload = false;
  1466. auto_reload_running_scripts = false;
  1467. editor = p_editor;
  1468. menu_hb = memnew(HBoxContainer);
  1469. add_child(menu_hb);
  1470. script_split = memnew(HSplitContainer);
  1471. add_child(script_split);
  1472. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1473. script_list = memnew(ItemList);
  1474. script_split->add_child(script_list);
  1475. script_list->set_custom_minimum_size(Size2(0, 0));
  1476. script_split->set_split_offset(140);
  1477. tab_container = memnew(TabContainer);
  1478. tab_container->set_tabs_visible(false);
  1479. script_split->add_child(tab_container);
  1480. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1481. ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_GREATER);
  1482. ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_LESS);
  1483. set_process_unhandled_input(true);
  1484. file_menu = memnew(MenuButton);
  1485. menu_hb->add_child(file_menu);
  1486. file_menu->set_text(TTR("File"));
  1487. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New")), FILE_NEW);
  1488. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open")), FILE_OPEN);
  1489. file_menu->get_popup()->add_separator();
  1490. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT | KEY_MASK_CMD | KEY_S), FILE_SAVE);
  1491. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As..")), FILE_SAVE_AS);
  1492. 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);
  1493. file_menu->get_popup()->add_separator();
  1494. 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);
  1495. file_menu->get_popup()->add_separator();
  1496. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_previous", TTR("History Prev"), KEY_MASK_ALT | KEY_LEFT), WINDOW_PREV);
  1497. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_next", TTR("History Next"), KEY_MASK_ALT | KEY_RIGHT), WINDOW_NEXT);
  1498. file_menu->get_popup()->add_separator();
  1499. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme")), FILE_IMPORT_THEME);
  1500. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), FILE_RELOAD_THEME);
  1501. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), FILE_SAVE_THEME);
  1502. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As")), FILE_SAVE_THEME_AS);
  1503. file_menu->get_popup()->add_separator();
  1504. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTR("Close Docs")), CLOSE_DOCS);
  1505. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD | KEY_W), FILE_CLOSE);
  1506. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_all", TTR("Close All")), CLOSE_ALL);
  1507. file_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  1508. script_search_menu = memnew(MenuButton);
  1509. menu_hb->add_child(script_search_menu);
  1510. script_search_menu->set_text(TTR("Search"));
  1511. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F), HELP_SEARCH_FIND);
  1512. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), HELP_SEARCH_FIND_NEXT);
  1513. script_search_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  1514. script_search_menu->hide();
  1515. debug_menu = memnew(MenuButton);
  1516. menu_hb->add_child(debug_menu);
  1517. debug_menu->set_text(TTR("Debug"));
  1518. debug_menu->get_popup()->add_separator();
  1519. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
  1520. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
  1521. debug_menu->get_popup()->add_separator();
  1522. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
  1523. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue")), DEBUG_CONTINUE);
  1524. debug_menu->get_popup()->add_separator();
  1525. //debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  1526. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
  1527. debug_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  1528. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1529. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1530. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
  1531. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1532. #if 0
  1533. window_menu = memnew( MenuButton );
  1534. menu_hb->add_child(window_menu);
  1535. window_menu->set_text(TTR("Window"));
  1536. window_menu->get_popup()->add_item(TTR("Close"),WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  1537. window_menu->get_popup()->add_separator();
  1538. window_menu->get_popup()->add_item(TTR("Move Left"),WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  1539. window_menu->get_popup()->add_item(TTR("Move Right"),WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  1540. window_menu->get_popup()->add_separator();
  1541. window_menu->get_popup()->connect("id_pressed", this,"_menu_option");
  1542. #endif
  1543. menu_hb->add_spacer();
  1544. script_icon = memnew(TextureRect);
  1545. menu_hb->add_child(script_icon);
  1546. script_name_label = memnew(Label);
  1547. menu_hb->add_child(script_name_label);
  1548. script_icon->hide();
  1549. script_name_label->hide();
  1550. menu_hb->add_spacer();
  1551. site_search = memnew(ToolButton);
  1552. site_search->set_text(TTR("Tutorials"));
  1553. site_search->connect("pressed", this, "_menu_option", varray(SEARCH_WEBSITE));
  1554. menu_hb->add_child(site_search);
  1555. site_search->set_tooltip(TTR("Open https://godotengine.org at tutorials section."));
  1556. class_search = memnew(ToolButton);
  1557. class_search->set_text(TTR("Classes"));
  1558. class_search->connect("pressed", this, "_menu_option", varray(SEARCH_CLASSES));
  1559. menu_hb->add_child(class_search);
  1560. class_search->set_tooltip(TTR("Search the class hierarchy."));
  1561. help_search = memnew(ToolButton);
  1562. help_search->set_text(TTR("Search Help"));
  1563. help_search->connect("pressed", this, "_menu_option", varray(SEARCH_HELP));
  1564. menu_hb->add_child(help_search);
  1565. help_search->set_tooltip(TTR("Search the reference documentation."));
  1566. menu_hb->add_child(memnew(VSeparator));
  1567. script_back = memnew(ToolButton);
  1568. script_back->connect("pressed", this, "_history_back");
  1569. menu_hb->add_child(script_back);
  1570. script_back->set_disabled(true);
  1571. script_back->set_tooltip(TTR("Go to previous edited document."));
  1572. script_forward = memnew(ToolButton);
  1573. script_forward->connect("pressed", this, "_history_forward");
  1574. menu_hb->add_child(script_forward);
  1575. script_forward->set_disabled(true);
  1576. script_forward->set_tooltip(TTR("Go to next edited document."));
  1577. tab_container->connect("tab_changed", this, "_tab_changed");
  1578. erase_tab_confirm = memnew(ConfirmationDialog);
  1579. erase_tab_confirm->get_ok()->set_text(TTR("Save"));
  1580. erase_tab_confirm->add_button(TTR("Discard"), OS::get_singleton()->get_swap_ok_cancel(), "discard");
  1581. erase_tab_confirm->connect("confirmed", this, "_close_current_tab");
  1582. erase_tab_confirm->connect("custom_action", this, "_close_discard_current_tab");
  1583. add_child(erase_tab_confirm);
  1584. script_create_dialog = memnew(ScriptCreateDialog);
  1585. script_create_dialog->set_title(TTR("Create Script"));
  1586. add_child(script_create_dialog);
  1587. script_create_dialog->connect("script_created", this, "_script_created");
  1588. file_dialog_option = -1;
  1589. file_dialog = memnew(EditorFileDialog);
  1590. add_child(file_dialog);
  1591. file_dialog->connect("file_selected", this, "_file_dialog_action");
  1592. debugger = memnew(ScriptEditorDebugger(editor));
  1593. debugger->connect("goto_script_line", this, "_goto_script_line");
  1594. debugger->connect("show_debugger", this, "_show_debugger");
  1595. disk_changed = memnew(ConfirmationDialog);
  1596. {
  1597. VBoxContainer *vbc = memnew(VBoxContainer);
  1598. disk_changed->add_child(vbc);
  1599. //disk_changed->set_child_rect(vbc);
  1600. Label *dl = memnew(Label);
  1601. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  1602. vbc->add_child(dl);
  1603. disk_changed_list = memnew(Tree);
  1604. vbc->add_child(disk_changed_list);
  1605. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1606. disk_changed->connect("confirmed", this, "_reload_scripts");
  1607. disk_changed->get_ok()->set_text(TTR("Reload"));
  1608. disk_changed->add_button(TTR("Resave"), !OS::get_singleton()->get_swap_ok_cancel(), "resave");
  1609. disk_changed->connect("custom_action", this, "_resave_scripts");
  1610. }
  1611. add_child(disk_changed);
  1612. script_editor = this;
  1613. Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"), debugger);
  1614. debugger->set_tool_button(db);
  1615. debugger->connect("breaked", this, "_breaked");
  1616. autosave_timer = memnew(Timer);
  1617. autosave_timer->set_one_shot(false);
  1618. add_child(autosave_timer);
  1619. grab_focus_block = false;
  1620. help_search_dialog = memnew(EditorHelpSearch);
  1621. add_child(help_search_dialog);
  1622. help_search_dialog->connect("go_to_help", this, "_help_class_goto");
  1623. help_index = memnew(EditorHelpIndex);
  1624. add_child(help_index);
  1625. help_index->connect("open_class", this, "_help_class_open");
  1626. history_pos = -1;
  1627. //debugger_gui->hide();
  1628. edit_pass = 0;
  1629. trim_trailing_whitespace_on_save = false;
  1630. ScriptServer::edit_request_func = _open_script_request;
  1631. }
  1632. ScriptEditor::~ScriptEditor() {
  1633. memdelete(completion_cache);
  1634. }
  1635. void ScriptEditorPlugin::edit(Object *p_object) {
  1636. if (!p_object->cast_to<Script>())
  1637. return;
  1638. script_editor->edit(p_object->cast_to<Script>());
  1639. }
  1640. bool ScriptEditorPlugin::handles(Object *p_object) const {
  1641. if (p_object->cast_to<Script>()) {
  1642. bool valid = _can_open_in_editor(p_object->cast_to<Script>());
  1643. if (!valid) { //user tried to open it by clicking
  1644. EditorNode::get_singleton()->show_warning(TTR("Built-in scripts can only be edited when the scene they belong to is loaded"));
  1645. }
  1646. return valid;
  1647. }
  1648. return p_object->is_class("Script");
  1649. }
  1650. void ScriptEditorPlugin::make_visible(bool p_visible) {
  1651. if (p_visible) {
  1652. script_editor->show();
  1653. script_editor->set_process(true);
  1654. script_editor->ensure_select_current();
  1655. } else {
  1656. script_editor->hide();
  1657. script_editor->set_process(false);
  1658. }
  1659. }
  1660. void ScriptEditorPlugin::selected_notify() {
  1661. script_editor->ensure_select_current();
  1662. }
  1663. Dictionary ScriptEditorPlugin::get_state() const {
  1664. return script_editor->get_state();
  1665. }
  1666. void ScriptEditorPlugin::set_state(const Dictionary &p_state) {
  1667. script_editor->set_state(p_state);
  1668. }
  1669. void ScriptEditorPlugin::clear() {
  1670. script_editor->clear();
  1671. }
  1672. void ScriptEditorPlugin::save_external_data() {
  1673. script_editor->save_all_scripts();
  1674. }
  1675. void ScriptEditorPlugin::apply_changes() {
  1676. script_editor->apply_scripts();
  1677. }
  1678. void ScriptEditorPlugin::restore_global_state() {
  1679. }
  1680. void ScriptEditorPlugin::save_global_state() {
  1681. }
  1682. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  1683. script_editor->set_window_layout(p_layout);
  1684. }
  1685. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  1686. script_editor->get_window_layout(p_layout);
  1687. }
  1688. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  1689. return script_editor->get_breakpoints(p_breakpoints);
  1690. }
  1691. void ScriptEditorPlugin::edited_scene_changed() {
  1692. script_editor->edited_scene_changed();
  1693. }
  1694. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  1695. editor = p_node;
  1696. script_editor = memnew(ScriptEditor(p_node));
  1697. editor->get_viewport()->add_child(script_editor);
  1698. script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1699. script_editor->hide();
  1700. EDITOR_DEF("text_editor/files/auto_reload_scripts_on_external_change", true);
  1701. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/files/auto_reload_and_parse_scripts_on_save", true));
  1702. EDITOR_DEF("text_editor/files/open_dominant_script_on_scene_change", true);
  1703. EDITOR_DEF("text_editor/external/use_external_editor", false);
  1704. EDITOR_DEF("text_editor/external/exec_path", "");
  1705. EDITOR_DEF("text_editor/open_scripts/script_temperature_enabled", true);
  1706. EDITOR_DEF("text_editor/open_scripts/highlight_current_script", true);
  1707. EDITOR_DEF("text_editor/open_scripts/script_temperature_history_size", 15);
  1708. EDITOR_DEF("text_editor/open_scripts/script_temperature_hot_color", Color(1, 0, 0, 0.3));
  1709. EDITOR_DEF("text_editor/open_scripts/script_temperature_cold_color", Color(0, 0, 1, 0.3));
  1710. EDITOR_DEF("text_editor/open_scripts/current_script_background_color", Color(0.81, 0.81, 0.14, 0.63));
  1711. EDITOR_DEF("text_editor/open_scripts/group_help_pages", true);
  1712. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/open_scripts/sort_scripts_by", PROPERTY_HINT_ENUM, "Name,Path"));
  1713. EDITOR_DEF("text_editor/open_scripts/sort_scripts_by", 0);
  1714. 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"));
  1715. EDITOR_DEF("text_editor/open_scripts/list_script_names_as", 0);
  1716. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "text_editor/external/exec_path", PROPERTY_HINT_GLOBAL_FILE));
  1717. EDITOR_DEF("text_editor/external/exec_flags", "");
  1718. }
  1719. ScriptEditorPlugin::~ScriptEditorPlugin() {
  1720. }