script_editor_plugin.cpp 64 KB

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