script_text_editor.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /*************************************************************************/
  2. /* script_text_editor.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_text_editor.h"
  30. #include "tools/editor/editor_settings.h"
  31. #include "os/keyboard.h"
  32. #include "tools/editor/script_editor_debugger.h"
  33. #include "tools/editor/editor_node.h"
  34. Vector<String> ScriptTextEditor::get_functions() {
  35. String errortxt;
  36. int line=-1,col;
  37. TextEdit *te=code_editor->get_text_edit();
  38. String text = te->get_text();
  39. List<String> fnc;
  40. if (script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  41. //if valid rewrite functions to latest
  42. functions.clear();
  43. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  44. functions.push_back(E->get());
  45. }
  46. }
  47. return functions;
  48. }
  49. void ScriptTextEditor::apply_code() {
  50. if (script.is_null())
  51. return;
  52. // print_line("applying code");
  53. script->set_source_code(code_editor->get_text_edit()->get_text());
  54. script->update_exports();
  55. }
  56. Ref<Script> ScriptTextEditor::get_edited_script() const {
  57. return script;
  58. }
  59. bool ScriptTextEditor::goto_method(const String& p_method) {
  60. Vector<String> functions = get_functions();
  61. String method_search = p_method + ":";
  62. for (int i=0;i<functions.size();i++) {
  63. String function=functions[i];
  64. if (function.begins_with(method_search)) {
  65. int line=function.get_slice(":",1).to_int();
  66. goto_line(line-1);
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. void ScriptTextEditor::_load_theme_settings() {
  73. TextEdit *text_edit = code_editor->get_text_edit();
  74. text_edit->clear_colors();
  75. /* keyword color */
  76. text_edit->set_custom_bg_color(EDITOR_DEF("text_editor/background_color",Color(0,0,0,0)));
  77. text_edit->add_color_override("completion_background_color", EDITOR_DEF("text_editor/completion_background_color", Color(0,0,0,0)));
  78. text_edit->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/completion_selected_color", Color::html("434244")));
  79. text_edit->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/completion_existing_color", Color::html("21dfdfdf")));
  80. text_edit->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/completion_scroll_color", Color::html("ffffff")));
  81. text_edit->add_color_override("completion_font_color", EDITOR_DEF("text_editor/completion_font_color", Color::html("aaaaaa")));
  82. text_edit->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
  83. text_edit->add_color_override("line_number_color",EDITOR_DEF("text_editor/line_number_color",Color(0,0,0)));
  84. text_edit->add_color_override("caret_color",EDITOR_DEF("text_editor/caret_color",Color(0,0,0)));
  85. text_edit->add_color_override("caret_background_color",EDITOR_DEF("text_editor/caret_background_color",Color(0,0,0)));
  86. text_edit->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
  87. text_edit->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
  88. text_edit->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
  89. text_edit->add_color_override("current_line_color",EDITOR_DEF("text_editor/current_line_color",Color(0.3,0.5,0.8,0.15)));
  90. text_edit->add_color_override("word_highlighted_color",EDITOR_DEF("text_editor/word_highlighted_color",Color(0.8,0.9,0.9,0.15)));
  91. text_edit->add_color_override("number_color",EDITOR_DEF("text_editor/number_color",Color(0.9,0.6,0.0,2)));
  92. text_edit->add_color_override("function_color",EDITOR_DEF("text_editor/function_color",Color(0.4,0.6,0.8)));
  93. text_edit->add_color_override("member_variable_color",EDITOR_DEF("text_editor/member_variable_color",Color(0.9,0.3,0.3)));
  94. text_edit->add_color_override("mark_color", EDITOR_DEF("text_editor/mark_color", Color(1.0,0.4,0.4,0.4)));
  95. text_edit->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/breakpoint_color", Color(0.8,0.8,0.4,0.2)));
  96. text_edit->add_color_override("search_result_color",EDITOR_DEF("text_editor/search_result_color",Color(0.05,0.25,0.05,1)));
  97. text_edit->add_color_override("search_result_border_color",EDITOR_DEF("text_editor/search_result_border_color",Color(0.1,0.45,0.1,1)));
  98. text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/line_spacing",4));
  99. Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));
  100. List<String> keywords;
  101. script->get_language()->get_reserved_words(&keywords);
  102. for(List<String>::Element *E=keywords.front();E;E=E->next()) {
  103. text_edit->add_keyword_color(E->get(),keyword_color);
  104. }
  105. //colorize core types
  106. Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  107. text_edit->add_keyword_color("Vector2",basetype_color);
  108. text_edit->add_keyword_color("Vector3",basetype_color);
  109. text_edit->add_keyword_color("Plane",basetype_color);
  110. text_edit->add_keyword_color("Quat",basetype_color);
  111. text_edit->add_keyword_color("AABB",basetype_color);
  112. text_edit->add_keyword_color("Matrix3",basetype_color);
  113. text_edit->add_keyword_color("Transform",basetype_color);
  114. text_edit->add_keyword_color("Color",basetype_color);
  115. text_edit->add_keyword_color("Image",basetype_color);
  116. text_edit->add_keyword_color("InputEvent",basetype_color);
  117. text_edit->add_keyword_color("Rect2",basetype_color);
  118. text_edit->add_keyword_color("NodePath",basetype_color);
  119. //colorize engine types
  120. Color type_color= EDITOR_DEF("text_editor/engine_type_color",Color(0.0,0.2,0.4));
  121. List<StringName> types;
  122. ObjectTypeDB::get_type_list(&types);
  123. for(List<StringName>::Element *E=types.front();E;E=E->next()) {
  124. String n = E->get();
  125. if (n.begins_with("_"))
  126. n = n.substr(1, n.length());
  127. text_edit->add_keyword_color(n,type_color);
  128. }
  129. //colorize comments
  130. Color comment_color = EDITOR_DEF("text_editor/comment_color",Color::hex(0x797e7eff));
  131. List<String> comments;
  132. script->get_language()->get_comment_delimiters(&comments);
  133. for(List<String>::Element *E=comments.front();E;E=E->next()) {
  134. String comment = E->get();
  135. String beg = comment.get_slice(" ",0);
  136. String end = comment.get_slice_count(" ")>1?comment.get_slice(" ",1):String();
  137. text_edit->add_color_region(beg,end,comment_color,end=="");
  138. }
  139. //colorize strings
  140. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  141. List<String> strings;
  142. script->get_language()->get_string_delimiters(&strings);
  143. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  144. String string = E->get();
  145. String beg = string.get_slice(" ",0);
  146. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  147. text_edit->add_color_region(beg,end,string_color,end=="");
  148. }
  149. //colorize symbols
  150. Color symbol_color= EDITOR_DEF("text_editor/symbol_color",Color::hex(0x005291ff));
  151. text_edit->set_symbol_color(symbol_color);
  152. }
  153. void ScriptTextEditor::reload_text() {
  154. ERR_FAIL_COND(script.is_null()) ;
  155. TextEdit *te = code_editor->get_text_edit();
  156. int column = te->cursor_get_column();
  157. int row = te->cursor_get_line();
  158. int h = te->get_h_scroll();
  159. int v = te->get_v_scroll();
  160. te->set_text(script->get_source_code());
  161. te->clear_undo_history();
  162. te->cursor_set_line(row);
  163. te->cursor_set_column(column);
  164. te->set_h_scroll(h);
  165. te->set_v_scroll(v);
  166. te->tag_saved_version();
  167. code_editor->update_line_and_column();
  168. }
  169. void ScriptTextEditor::_notification(int p_what) {
  170. if (p_what==NOTIFICATION_READY) {
  171. //emit_signal("name_changed");
  172. }
  173. }
  174. void ScriptTextEditor::add_callback(const String& p_function,StringArray p_args) {
  175. String code = code_editor->get_text_edit()->get_text();
  176. int pos = script->get_language()->find_function(p_function,code);
  177. if (pos==-1) {
  178. //does not exist
  179. code_editor->get_text_edit()->deselect();
  180. pos=code_editor->get_text_edit()->get_line_count()+2;
  181. String func = script->get_language()->make_function("",p_function,p_args);
  182. //code=code+func;
  183. code_editor->get_text_edit()->cursor_set_line(pos+1);
  184. code_editor->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  185. code_editor->get_text_edit()->insert_text_at_cursor("\n\n"+func);
  186. }
  187. code_editor->get_text_edit()->cursor_set_line(pos);
  188. code_editor->get_text_edit()->cursor_set_column(1);
  189. }
  190. void ScriptTextEditor::update_settings() {
  191. code_editor->update_editor_settings();
  192. }
  193. bool ScriptTextEditor::is_unsaved() {
  194. return code_editor->get_text_edit()->get_version()!=code_editor->get_text_edit()->get_saved_version();
  195. }
  196. Variant ScriptTextEditor::get_edit_state() {
  197. Dictionary state;
  198. state["scroll_pos"]=code_editor->get_text_edit()->get_v_scroll();
  199. state["column"]=code_editor->get_text_edit()->cursor_get_column();
  200. state["row"]=code_editor->get_text_edit()->cursor_get_line();
  201. return state;
  202. }
  203. void ScriptTextEditor::trim_trailing_whitespace() {
  204. TextEdit *tx = code_editor->get_text_edit();
  205. bool trimed_whitespace = false;
  206. for (int i = 0; i < tx->get_line_count(); i++) {
  207. String line = tx->get_line(i);
  208. if (line.ends_with(" ") || line.ends_with("\t")) {
  209. if (!trimed_whitespace) {
  210. tx->begin_complex_operation();
  211. trimed_whitespace = true;
  212. }
  213. int end = 0;
  214. for (int j = line.length() - 1; j > -1; j--) {
  215. if (line[j] != ' ' && line[j] != '\t') {
  216. end = j+1;
  217. break;
  218. }
  219. }
  220. tx->set_line(i, line.substr(0, end));
  221. }
  222. }
  223. if (trimed_whitespace) {
  224. tx->end_complex_operation();
  225. tx->update();
  226. }
  227. }
  228. void ScriptTextEditor::tag_saved_version() {
  229. code_editor->get_text_edit()->tag_saved_version();
  230. }
  231. void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
  232. code_editor->get_text_edit()->cursor_set_line(p_line);
  233. }
  234. void ScriptTextEditor::ensure_focus() {
  235. code_editor->get_text_edit()->grab_focus();
  236. }
  237. void ScriptTextEditor::set_edit_state(const Variant& p_state) {
  238. Dictionary state=p_state;
  239. code_editor->get_text_edit()->set_v_scroll(state["scroll_pos"]);
  240. code_editor->get_text_edit()->cursor_set_column( state["column"]);
  241. code_editor->get_text_edit()->cursor_set_line( state["row"] );
  242. code_editor->get_text_edit()->grab_focus();
  243. //int scroll_pos;
  244. //int cursor_column;
  245. //int cursor_row;
  246. }
  247. String ScriptTextEditor::get_name() {
  248. String name;
  249. if (script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  250. name=script->get_path().get_file();
  251. if (is_unsaved()) {
  252. name+="(*)";
  253. }
  254. } else if (script->get_name()!="")
  255. name=script->get_name();
  256. else
  257. name=script->get_type()+"("+itos(script->get_instance_ID())+")";
  258. return name;
  259. }
  260. Ref<Texture> ScriptTextEditor::get_icon() {
  261. if (get_parent_control() && get_parent_control()->has_icon(script->get_type(),"EditorIcons")) {
  262. return get_parent_control()->get_icon(script->get_type(),"EditorIcons");
  263. }
  264. return Ref<Texture>();
  265. }
  266. void ScriptTextEditor::set_edited_script(const Ref<Script>& p_script) {
  267. ERR_FAIL_COND(!script.is_null());
  268. script=p_script;
  269. _load_theme_settings();
  270. code_editor->get_text_edit()->set_text(script->get_source_code());
  271. code_editor->get_text_edit()->clear_undo_history();
  272. code_editor->get_text_edit()->tag_saved_version();
  273. emit_signal("name_changed");
  274. code_editor->update_line_and_column();
  275. }
  276. void ScriptTextEditor::_validate_script() {
  277. String errortxt;
  278. int line=-1,col;
  279. TextEdit *te=code_editor->get_text_edit();
  280. String text = te->get_text();
  281. List<String> fnc;
  282. if (!script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  283. String error_text="error("+itos(line)+","+itos(col)+"): "+errortxt;
  284. code_editor->set_error(error_text);
  285. } else {
  286. code_editor->set_error("");
  287. line=-1;
  288. if (!script->is_tool()) {
  289. script->set_source_code(text);
  290. script->update_exports();
  291. //script->reload(); //will update all the variables in property editors
  292. }
  293. functions.clear();
  294. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  295. functions.push_back(E->get());
  296. }
  297. }
  298. line--;
  299. for(int i=0;i<te->get_line_count();i++) {
  300. te->set_line_as_marked(i,line==i);
  301. }
  302. emit_signal("name_changed");
  303. }
  304. static Node* _find_node_for_script(Node* p_base, Node*p_current, const Ref<Script>& p_script) {
  305. if (p_current->get_owner()!=p_base && p_base!=p_current)
  306. return NULL;
  307. Ref<Script> c = p_current->get_script();
  308. if (c==p_script)
  309. return p_current;
  310. for(int i=0;i<p_current->get_child_count();i++) {
  311. Node *found = _find_node_for_script(p_base,p_current->get_child(i),p_script);
  312. if (found)
  313. return found;
  314. }
  315. return NULL;
  316. }
  317. static void _find_changed_scripts_for_external_editor(Node* p_base, Node*p_current, Set<Ref<Script> > &r_scripts) {
  318. if (p_current->get_owner()!=p_base && p_base!=p_current)
  319. return;
  320. Ref<Script> c = p_current->get_script();
  321. if (c.is_valid())
  322. r_scripts.insert(c);
  323. for(int i=0;i<p_current->get_child_count();i++) {
  324. _find_changed_scripts_for_external_editor(p_base,p_current->get_child(i),r_scripts);
  325. }
  326. }
  327. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  328. if (!bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  329. return;
  330. Set<Ref<Script> > scripts;
  331. Node *base = get_tree()->get_edited_scene_root();
  332. if (base) {
  333. _find_changed_scripts_for_external_editor(base,base,scripts);
  334. }
  335. for (Set<Ref<Script> >::Element *E=scripts.front();E;E=E->next()) {
  336. Ref<Script> script = E->get();
  337. if (p_for_script.is_valid() && p_for_script!=script)
  338. continue;
  339. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  340. continue; //internal script, who cares, though weird
  341. }
  342. uint64_t last_date = script->get_last_modified_time();
  343. uint64_t date = FileAccess::get_modified_time(script->get_path());
  344. if (last_date!=date) {
  345. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  346. ERR_CONTINUE(!rel_script.is_valid());
  347. script->set_source_code( rel_script->get_source_code() );
  348. script->set_last_modified_time( rel_script->get_last_modified_time() );
  349. script->update_exports();
  350. }
  351. }
  352. }
  353. void ScriptTextEditor::_code_complete_scripts(void* p_ud,const String& p_code, List<String>* r_options) {
  354. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  355. ste->_code_complete_script(p_code,r_options);
  356. }
  357. void ScriptTextEditor::_code_complete_script(const String& p_code, List<String>* r_options) {
  358. if (color_panel->is_visible()) return;
  359. Node *base = get_tree()->get_edited_scene_root();
  360. if (base) {
  361. base = _find_node_for_script(base,base,script);
  362. }
  363. String hint;
  364. Error err = script->get_language()->complete_code(p_code,script->get_path().get_base_dir(),base,r_options,hint);
  365. if (hint!="") {
  366. code_editor->get_text_edit()->set_code_hint(hint);
  367. }
  368. }
  369. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  370. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(),p_row+1,code_editor->get_text_edit()->is_line_set_as_breakpoint(p_row));
  371. }
  372. static void swap_lines(TextEdit *tx, int line1, int line2)
  373. {
  374. String tmp = tx->get_line(line1);
  375. String tmp2 = tx->get_line(line2);
  376. tx->set_line(line2, tmp);
  377. tx->set_line(line1, tmp2);
  378. tx->cursor_set_line(line2);
  379. }
  380. void ScriptTextEditor::_lookup_symbol(const String& p_symbol,int p_row, int p_column) {
  381. Node *base = get_tree()->get_edited_scene_root();
  382. if (base) {
  383. base = _find_node_for_script(base,base,script);
  384. }
  385. ScriptLanguage::LookupResult result;
  386. if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(),p_symbol,script->get_path().get_base_dir(),base,result)==OK) {
  387. _goto_line(p_row);
  388. switch(result.type) {
  389. case ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION: {
  390. if (result.script.is_valid()) {
  391. emit_signal("request_open_script_at_line",result.script,result.location-1);
  392. } else {
  393. emit_signal("request_save_history");
  394. _goto_line(result.location-1);
  395. }
  396. } break;
  397. case ScriptLanguage::LookupResult::RESULT_CLASS: {
  398. emit_signal("go_to_help","class_name:"+result.class_name);
  399. } break;
  400. case ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT: {
  401. StringName cname = result.class_name;
  402. bool success;
  403. while(true) {
  404. ObjectTypeDB::get_integer_constant(cname,result.class_member,&success);
  405. if (success) {
  406. result.class_name=cname;
  407. cname=ObjectTypeDB::type_inherits_from(cname);
  408. } else {
  409. break;
  410. }
  411. }
  412. emit_signal("go_to_help","class_constant:"+result.class_name+":"+result.class_member);
  413. } break;
  414. case ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY: {
  415. emit_signal("go_to_help","class_property:"+result.class_name+":"+result.class_member);
  416. } break;
  417. case ScriptLanguage::LookupResult::RESULT_CLASS_METHOD: {
  418. StringName cname = result.class_name;
  419. while(true) {
  420. if (ObjectTypeDB::has_method(cname,result.class_member)) {
  421. result.class_name=cname;
  422. cname=ObjectTypeDB::type_inherits_from(cname);
  423. } else {
  424. break;
  425. }
  426. }
  427. emit_signal("go_to_help","class_method:"+result.class_name+":"+result.class_member);
  428. } break;
  429. }
  430. }
  431. }
  432. void ScriptTextEditor::_edit_option(int p_op) {
  433. switch(p_op) {
  434. case EDIT_UNDO: {
  435. code_editor->get_text_edit()->undo();
  436. code_editor->get_text_edit()->call_deferred("grab_focus");
  437. } break;
  438. case EDIT_REDO: {
  439. code_editor->get_text_edit()->redo();
  440. code_editor->get_text_edit()->call_deferred("grab_focus");
  441. } break;
  442. case EDIT_CUT: {
  443. code_editor->get_text_edit()->cut();
  444. code_editor->get_text_edit()->call_deferred("grab_focus");
  445. } break;
  446. case EDIT_COPY: {
  447. code_editor->get_text_edit()->copy();
  448. code_editor->get_text_edit()->call_deferred("grab_focus");
  449. } break;
  450. case EDIT_PASTE: {
  451. code_editor->get_text_edit()->paste();
  452. code_editor->get_text_edit()->call_deferred("grab_focus");
  453. } break;
  454. case EDIT_SELECT_ALL: {
  455. code_editor->get_text_edit()->select_all();
  456. code_editor->get_text_edit()->call_deferred("grab_focus");
  457. } break;
  458. case EDIT_MOVE_LINE_UP: {
  459. TextEdit *tx = code_editor->get_text_edit();
  460. Ref<Script> scr = script;
  461. if (scr.is_null())
  462. return;
  463. tx->begin_complex_operation();
  464. if (tx->is_selection_active())
  465. {
  466. int from_line = tx->get_selection_from_line();
  467. int from_col = tx->get_selection_from_column();
  468. int to_line = tx->get_selection_to_line();
  469. int to_column = tx->get_selection_to_column();
  470. for (int i = from_line; i <= to_line; i++)
  471. {
  472. int line_id = i;
  473. int next_id = i - 1;
  474. if (line_id == 0 || next_id < 0)
  475. return;
  476. swap_lines(tx, line_id, next_id);
  477. }
  478. int from_line_up = from_line > 0 ? from_line-1 : from_line;
  479. int to_line_up = to_line > 0 ? to_line-1 : to_line;
  480. tx->select(from_line_up, from_col, to_line_up, to_column);
  481. }
  482. else
  483. {
  484. int line_id = tx->cursor_get_line();
  485. int next_id = line_id - 1;
  486. if (line_id == 0 || next_id < 0)
  487. return;
  488. swap_lines(tx, line_id, next_id);
  489. }
  490. tx->end_complex_operation();
  491. tx->update();
  492. } break;
  493. case EDIT_MOVE_LINE_DOWN: {
  494. TextEdit *tx = code_editor->get_text_edit();
  495. Ref<Script> scr = get_edited_script();
  496. if (scr.is_null())
  497. return;
  498. tx->begin_complex_operation();
  499. if (tx->is_selection_active())
  500. {
  501. int from_line = tx->get_selection_from_line();
  502. int from_col = tx->get_selection_from_column();
  503. int to_line = tx->get_selection_to_line();
  504. int to_column = tx->get_selection_to_column();
  505. for (int i = to_line; i >= from_line; i--)
  506. {
  507. int line_id = i;
  508. int next_id = i + 1;
  509. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  510. return;
  511. swap_lines(tx, line_id, next_id);
  512. }
  513. int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
  514. int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
  515. tx->select(from_line_down, from_col, to_line_down, to_column);
  516. }
  517. else
  518. {
  519. int line_id = tx->cursor_get_line();
  520. int next_id = line_id + 1;
  521. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  522. return;
  523. swap_lines(tx, line_id, next_id);
  524. }
  525. tx->end_complex_operation();
  526. tx->update();
  527. } break;
  528. case EDIT_INDENT_LEFT: {
  529. TextEdit *tx = code_editor->get_text_edit();
  530. Ref<Script> scr = get_edited_script();
  531. if (scr.is_null())
  532. return;
  533. tx->begin_complex_operation();
  534. if (tx->is_selection_active())
  535. {
  536. tx->indent_selection_left();
  537. }
  538. else
  539. {
  540. int begin = tx->cursor_get_line();
  541. String line_text = tx->get_line(begin);
  542. // begins with tab
  543. if (line_text.begins_with("\t"))
  544. {
  545. line_text = line_text.substr(1, line_text.length());
  546. tx->set_line(begin, line_text);
  547. }
  548. // begins with 4 spaces
  549. else if (line_text.begins_with(" "))
  550. {
  551. line_text = line_text.substr(4, line_text.length());
  552. tx->set_line(begin, line_text);
  553. }
  554. }
  555. tx->end_complex_operation();
  556. tx->update();
  557. //tx->deselect();
  558. } break;
  559. case EDIT_INDENT_RIGHT: {
  560. TextEdit *tx = code_editor->get_text_edit();
  561. Ref<Script> scr = get_edited_script();
  562. if (scr.is_null())
  563. return;
  564. tx->begin_complex_operation();
  565. if (tx->is_selection_active())
  566. {
  567. tx->indent_selection_right();
  568. }
  569. else
  570. {
  571. int begin = tx->cursor_get_line();
  572. String line_text = tx->get_line(begin);
  573. line_text = '\t' + line_text;
  574. tx->set_line(begin, line_text);
  575. }
  576. tx->end_complex_operation();
  577. tx->update();
  578. //tx->deselect();
  579. } break;
  580. case EDIT_CLONE_DOWN: {
  581. TextEdit *tx = code_editor->get_text_edit();
  582. Ref<Script> scr = get_edited_script();
  583. if (scr.is_null())
  584. return;
  585. int from_line = tx->cursor_get_line();
  586. int to_line = tx->cursor_get_line();
  587. int column = tx->cursor_get_column();
  588. if (tx->is_selection_active()) {
  589. from_line = tx->get_selection_from_line();
  590. to_line = tx->get_selection_to_line();
  591. column = tx->cursor_get_column();
  592. }
  593. int next_line = to_line + 1;
  594. tx->begin_complex_operation();
  595. for (int i = from_line; i <= to_line; i++) {
  596. if (i >= tx->get_line_count() - 1) {
  597. tx->set_line(i, tx->get_line(i) + "\n");
  598. }
  599. String line_clone = tx->get_line(i);
  600. tx->insert_at(line_clone, next_line);
  601. next_line++;
  602. }
  603. tx->cursor_set_column(column);
  604. if (tx->is_selection_active()) {
  605. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  606. }
  607. tx->end_complex_operation();
  608. tx->update();
  609. } break;
  610. case EDIT_TOGGLE_COMMENT: {
  611. TextEdit *tx = code_editor->get_text_edit();
  612. Ref<Script> scr = get_edited_script();
  613. if (scr.is_null())
  614. return;
  615. tx->begin_complex_operation();
  616. if (tx->is_selection_active())
  617. {
  618. int begin = tx->get_selection_from_line();
  619. int end = tx->get_selection_to_line();
  620. // End of selection ends on the first column of the last line, ignore it.
  621. if(tx->get_selection_to_column() == 0)
  622. end -= 1;
  623. for (int i = begin; i <= end; i++)
  624. {
  625. String line_text = tx->get_line(i);
  626. if (line_text.begins_with("#"))
  627. line_text = line_text.substr(1, line_text.length());
  628. else
  629. line_text = "#" + line_text;
  630. tx->set_line(i, line_text);
  631. }
  632. }
  633. else
  634. {
  635. int begin = tx->cursor_get_line();
  636. String line_text = tx->get_line(begin);
  637. if (line_text.begins_with("#"))
  638. line_text = line_text.substr(1, line_text.length());
  639. else
  640. line_text = "#" + line_text;
  641. tx->set_line(begin, line_text);
  642. }
  643. tx->end_complex_operation();
  644. tx->update();
  645. //tx->deselect();
  646. } break;
  647. case EDIT_COMPLETE: {
  648. code_editor->get_text_edit()->query_code_comple();
  649. } break;
  650. case EDIT_AUTO_INDENT: {
  651. TextEdit *te = code_editor->get_text_edit();
  652. String text = te->get_text();
  653. Ref<Script> scr = get_edited_script();
  654. if (scr.is_null())
  655. return;
  656. int begin,end;
  657. if (te->is_selection_active()) {
  658. begin=te->get_selection_from_line();
  659. end=te->get_selection_to_line();
  660. } else {
  661. begin=0;
  662. end=te->get_line_count()-1;
  663. }
  664. scr->get_language()->auto_indent_code(text,begin,end);
  665. te->set_text(text);
  666. } break;
  667. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  668. trim_trailing_whitespace();
  669. } break;
  670. case EDIT_PICK_COLOR: {
  671. color_panel->popup();
  672. } break;
  673. case SEARCH_FIND: {
  674. code_editor->get_find_replace_bar()->popup_search();
  675. } break;
  676. case SEARCH_FIND_NEXT: {
  677. code_editor->get_find_replace_bar()->search_next();
  678. } break;
  679. case SEARCH_FIND_PREV: {
  680. code_editor->get_find_replace_bar()->search_prev();
  681. } break;
  682. case SEARCH_REPLACE: {
  683. code_editor->get_find_replace_bar()->popup_replace();
  684. } break;
  685. case SEARCH_LOCATE_FUNCTION: {
  686. quick_open->popup(get_functions());
  687. } break;
  688. case SEARCH_GOTO_LINE: {
  689. goto_line_dialog->popup_find_line(code_editor->get_text_edit());
  690. } break;
  691. case DEBUG_TOGGLE_BREAKPOINT: {
  692. int line=code_editor->get_text_edit()->cursor_get_line();
  693. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  694. code_editor->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  695. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(),line+1,dobreak);
  696. } break;
  697. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  698. List<int> bpoints;
  699. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  700. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  701. int line = E->get();
  702. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  703. code_editor->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  704. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(),line+1,dobreak);
  705. }
  706. }
  707. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  708. List<int> bpoints;
  709. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  710. if (bpoints.size() <= 0) {
  711. return;
  712. }
  713. int line=code_editor->get_text_edit()->cursor_get_line();
  714. // wrap around
  715. if (line >= bpoints[bpoints.size() - 1]) {
  716. code_editor->get_text_edit()->cursor_set_line(bpoints[0]);
  717. } else {
  718. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  719. int bline = E->get();
  720. if (bline > line) {
  721. code_editor->get_text_edit()->cursor_set_line(bline);
  722. return;
  723. }
  724. }
  725. }
  726. } break;
  727. case DEBUG_GOTO_PREV_BREAKPOINT: {
  728. List<int> bpoints;
  729. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  730. if (bpoints.size() <= 0) {
  731. return;
  732. }
  733. int line=code_editor->get_text_edit()->cursor_get_line();
  734. // wrap around
  735. if (line <= bpoints[0]) {
  736. code_editor->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  737. } else {
  738. for(List<int>::Element *E=bpoints.back();E;E=E->prev()) {
  739. int bline = E->get();
  740. if (bline < line) {
  741. code_editor->get_text_edit()->cursor_set_line(bline);
  742. return;
  743. }
  744. }
  745. }
  746. } break;
  747. case HELP_CONTEXTUAL: {
  748. String text = code_editor->get_text_edit()->get_selection_text();
  749. if (text == "")
  750. text = code_editor->get_text_edit()->get_word_under_cursor();
  751. if (text != "") {
  752. emit_signal("request_help_search",text);
  753. }
  754. } break;
  755. }
  756. }
  757. void ScriptTextEditor::_bind_methods() {
  758. ObjectTypeDB::bind_method("_validate_script",&ScriptTextEditor::_validate_script);
  759. ObjectTypeDB::bind_method("_load_theme_settings",&ScriptTextEditor::_load_theme_settings);
  760. ObjectTypeDB::bind_method("_breakpoint_toggled",&ScriptTextEditor::_breakpoint_toggled);
  761. ObjectTypeDB::bind_method("_edit_option",&ScriptTextEditor::_edit_option);
  762. ObjectTypeDB::bind_method("_goto_line",&ScriptTextEditor::_goto_line);
  763. ObjectTypeDB::bind_method("_lookup_symbol",&ScriptTextEditor::_lookup_symbol);
  764. ObjectTypeDB::bind_method("_text_edit_input_event", &ScriptTextEditor::_text_edit_input_event);
  765. ObjectTypeDB::bind_method("_color_changed", &ScriptTextEditor::_color_changed);
  766. ObjectTypeDB::bind_method("get_drag_data_fw",&ScriptTextEditor::get_drag_data_fw);
  767. ObjectTypeDB::bind_method("can_drop_data_fw",&ScriptTextEditor::can_drop_data_fw);
  768. ObjectTypeDB::bind_method("drop_data_fw",&ScriptTextEditor::drop_data_fw);
  769. }
  770. Control *ScriptTextEditor::get_edit_menu() {
  771. return edit_hb;
  772. }
  773. void ScriptTextEditor::reload(bool p_soft) {
  774. TextEdit *te = code_editor->get_text_edit();
  775. Ref<Script> scr = get_edited_script();
  776. if (scr.is_null())
  777. return;
  778. scr->set_source_code(te->get_text());
  779. bool soft = p_soft || scr->get_instance_base_type()=="EditorPlugin"; //always soft-reload editor plugins
  780. scr->get_language()->reload_tool_script(scr,soft);
  781. }
  782. void ScriptTextEditor::get_breakpoints(List<int> *p_breakpoints) {
  783. code_editor->get_text_edit()->get_breakpoints(p_breakpoints);
  784. }
  785. void ScriptTextEditor::set_tooltip_request_func(String p_method,Object* p_obj) {
  786. code_editor->get_text_edit()->set_tooltip_request_func(p_obj,p_method,this);
  787. }
  788. void ScriptTextEditor::set_debugger_active(bool p_active) {
  789. }
  790. Variant ScriptTextEditor::get_drag_data_fw(const Point2& p_point,Control* p_from) {
  791. return Variant();
  792. }
  793. bool ScriptTextEditor::can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const{
  794. Dictionary d = p_data;
  795. if (d.has("type") &&
  796. (
  797. String(d["type"])=="resource" ||
  798. String(d["type"])=="files" ||
  799. String(d["type"])=="nodes"
  800. ) ) {
  801. return true;
  802. }
  803. return false;
  804. }
  805. #ifdef TOOLS_ENABLED
  806. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  807. if (p_edited_scene!=p_current_node && p_current_node->get_owner()!=p_edited_scene)
  808. return NULL;
  809. Ref<Script> scr = p_current_node->get_script();
  810. if (scr.is_valid() && scr==script)
  811. return p_current_node;
  812. for(int i=0;i<p_current_node->get_child_count();i++) {
  813. Node *n = _find_script_node(p_edited_scene,p_current_node->get_child(i),script);
  814. if (n)
  815. return n;
  816. }
  817. return NULL;
  818. }
  819. #else
  820. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  821. return NULL;
  822. }
  823. #endif
  824. void ScriptTextEditor::drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from){
  825. Dictionary d = p_data;
  826. if (d.has("type") && String(d["type"])=="resource") {
  827. Ref<Resource> res = d["resource"];
  828. if (!res.is_valid()) {
  829. return;
  830. }
  831. if (res->get_path().is_resource_file()) {
  832. EditorNode::get_singleton()->show_warning("Only resources from filesystem can be dropped.");
  833. return;
  834. }
  835. code_editor->get_text_edit()->insert_text_at_cursor(res->get_path());
  836. }
  837. if (d.has("type") && String(d["type"])=="files") {
  838. Array files = d["files"];
  839. String text_to_drop;
  840. for(int i=0;i<files.size();i++) {
  841. if (i>0)
  842. text_to_drop+=",";
  843. text_to_drop+="\""+String(files[i]).c_escape()+"\"";
  844. }
  845. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  846. }
  847. if (d.has("type") && String(d["type"])=="nodes") {
  848. Node* sn = _find_script_node(get_tree()->get_edited_scene_root(),get_tree()->get_edited_scene_root(),script);
  849. if (!sn) {
  850. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '"+get_name()+"' is not used in this scene.");
  851. return;
  852. }
  853. Array nodes = d["nodes"];
  854. String text_to_drop;
  855. for(int i=0;i<nodes.size();i++) {
  856. if (i>0)
  857. text_to_drop+=",";
  858. NodePath np = nodes[i];
  859. Node *node = get_node(np);
  860. if (!node) {
  861. continue;
  862. }
  863. String path = sn->get_path_to(node);
  864. text_to_drop+="\""+path.c_escape()+"\"";
  865. }
  866. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  867. }
  868. }
  869. void ScriptTextEditor::_text_edit_input_event(const InputEvent& ev) {
  870. if (ev.type == InputEvent::MOUSE_BUTTON) {
  871. InputEventMouseButton mb = ev.mouse_button;
  872. if (mb.button_index == BUTTON_RIGHT && !mb.pressed) {
  873. int col, row;
  874. TextEdit* tx = code_editor->get_text_edit();
  875. tx->_get_mouse_pos(Point2i(mb.global_x, mb.global_y)-tx->get_global_pos(), row, col);
  876. Vector2 mpos = Vector2(mb.global_x, mb.global_y)-tx->get_global_pos();
  877. bool have_selection = (tx->get_selection_text().length() > 0);
  878. bool have_color = (tx->get_word_at_pos(mpos) == "Color");
  879. if (have_color) {
  880. String line = tx->get_line(row);
  881. color_line = row;
  882. int begin = 0;
  883. int end = 0;
  884. bool valid = false;
  885. for (int i = col; i < line.length(); i++) {
  886. if (line[i] == '(') {
  887. begin = i;
  888. continue;
  889. }
  890. else if (line[i] == ')') {
  891. end = i+1;
  892. valid = true;
  893. break;
  894. }
  895. }
  896. if (valid) {
  897. color_args = line.substr(begin, end-begin);
  898. String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
  899. Vector<float> color = stripped.split_floats(",");
  900. if (color.size() > 2) {
  901. float alpha = color.size() > 3 ? color[3] : 1.0f;
  902. color_picker->set_color(Color(color[0], color[1], color[2], alpha));
  903. }
  904. color_panel->set_pos(get_global_transform().xform(get_local_mouse_pos()));
  905. Size2 ms = Size2(300, color_picker->get_combined_minimum_size().height+10);
  906. color_panel->set_size(ms);
  907. } else {
  908. have_color = false;
  909. }
  910. }
  911. _make_context_menu(have_selection, have_color);
  912. }
  913. }
  914. }
  915. void ScriptTextEditor::_color_changed(const Color& p_color) {
  916. String new_args;
  917. if (p_color.a == 1.0f) {
  918. new_args = String("("+rtos(p_color.r)+", "+rtos(p_color.g)+", "+rtos(p_color.b)+")");
  919. } else {
  920. new_args = String("("+rtos(p_color.r)+", "+rtos(p_color.g)+", "+rtos(p_color.b)+", "+rtos(p_color.a)+")");
  921. }
  922. String line = code_editor->get_text_edit()->get_line(color_line);
  923. String new_line = line.replace(color_args, new_args);
  924. color_args = new_args;
  925. code_editor->get_text_edit()->set_line(color_line, new_line);
  926. }
  927. void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color) {
  928. context_menu->clear();
  929. if (p_selection) {
  930. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"));
  931. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"));
  932. }
  933. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"));
  934. context_menu->add_separator();
  935. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"));
  936. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"));
  937. if (p_selection) {
  938. context_menu->add_separator();
  939. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"));
  940. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"));
  941. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"));
  942. }
  943. if (p_color) {
  944. context_menu->add_separator();
  945. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  946. }
  947. context_menu->set_pos(get_global_transform().xform(get_local_mouse_pos()));
  948. context_menu->set_size(Vector2(1, 1));
  949. context_menu->popup();
  950. }
  951. ScriptTextEditor::ScriptTextEditor() {
  952. code_editor = memnew( CodeTextEditor );
  953. add_child(code_editor);
  954. code_editor->set_area_as_parent_rect();
  955. code_editor->connect("validate_script",this,"_validate_script");
  956. code_editor->connect("load_theme_settings",this,"_load_theme_settings");
  957. code_editor->set_code_complete_func(_code_complete_scripts,this);
  958. code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
  959. code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol");
  960. update_settings();
  961. code_editor->get_text_edit()->set_callhint_settings(
  962. EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
  963. EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
  964. code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
  965. code_editor->get_text_edit()->set_context_menu_enabled(false);
  966. code_editor->get_text_edit()->connect("input_event", this, "_text_edit_input_event");
  967. context_menu = memnew(PopupMenu);
  968. add_child(context_menu);
  969. context_menu->connect("item_pressed", this, "_edit_option");
  970. color_panel = memnew(PopupPanel);
  971. add_child(color_panel);
  972. color_picker = memnew(ColorPicker);
  973. color_panel->add_child(color_picker);
  974. color_panel->set_child_rect(color_picker);
  975. color_picker->connect("color_changed", this, "_color_changed");
  976. edit_hb = memnew (HBoxContainer);
  977. edit_menu = memnew( MenuButton );
  978. edit_menu->set_text(TTR("Edit"));
  979. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  980. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  981. edit_menu->get_popup()->add_separator();
  982. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  983. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  984. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  985. edit_menu->get_popup()->add_separator();
  986. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  987. edit_menu->get_popup()->add_separator();
  988. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  989. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  990. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  991. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  992. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  993. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  994. edit_menu->get_popup()->add_separator();
  995. #ifdef OSX_ENABLED
  996. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  997. #else
  998. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  999. #endif
  1000. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  1001. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  1002. edit_menu->get_popup()->connect("item_pressed", this,"_edit_option");
  1003. edit_menu->get_popup()->add_separator();
  1004. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  1005. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  1006. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  1007. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  1008. search_menu = memnew( MenuButton );
  1009. edit_hb->add_child(search_menu);
  1010. search_menu->set_text(TTR("Search"));
  1011. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  1012. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  1013. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  1014. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  1015. search_menu->get_popup()->add_separator();
  1016. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  1017. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  1018. search_menu->get_popup()->add_separator();
  1019. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  1020. search_menu->get_popup()->connect("item_pressed", this,"_edit_option");
  1021. edit_hb->add_child(edit_menu);
  1022. quick_open = memnew( ScriptEditorQuickOpen );
  1023. add_child(quick_open);
  1024. quick_open->connect("goto_line",this,"_goto_line");
  1025. goto_line_dialog = memnew(GotoLineDialog);
  1026. add_child(goto_line_dialog);
  1027. code_editor->get_text_edit()->set_drag_forwarding(this);
  1028. }
  1029. static ScriptEditorBase * create_editor(const Ref<Script>& p_script) {
  1030. if (p_script->has_source_code()) {
  1031. return memnew( ScriptTextEditor );
  1032. }
  1033. return NULL;
  1034. }
  1035. void ScriptTextEditor::register_editor() {
  1036. ED_SHORTCUT("script_text_editor/undo", TTR("Undo"), KEY_MASK_CMD|KEY_Z);
  1037. ED_SHORTCUT("script_text_editor/redo", TTR("Redo"), KEY_MASK_CMD|KEY_Y);
  1038. ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD|KEY_X);
  1039. ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD|KEY_C);
  1040. ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD|KEY_V);
  1041. ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD|KEY_A);
  1042. ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT|KEY_UP);
  1043. ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT|KEY_DOWN);
  1044. ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), KEY_MASK_ALT|KEY_LEFT);
  1045. ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), KEY_MASK_ALT|KEY_RIGHT);
  1046. ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD|KEY_K);
  1047. ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD|KEY_B);
  1048. #ifdef OSX_ENABLED
  1049. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL|KEY_SPACE);
  1050. #else
  1051. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD|KEY_SPACE);
  1052. #endif
  1053. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL|KEY_MASK_ALT|KEY_T);
  1054. ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD|KEY_I);
  1055. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  1056. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL|KEY_MASK_SHIFT|KEY_F9);
  1057. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL|KEY_PERIOD);
  1058. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL|KEY_COMMA);
  1059. ED_SHORTCUT("script_text_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F);
  1060. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
  1061. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT|KEY_F3);
  1062. ED_SHORTCUT("script_text_editor/replace", TTR("Replace.."), KEY_MASK_CMD|KEY_R);
  1063. ED_SHORTCUT("script_text_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT|KEY_MASK_CMD|KEY_F);
  1064. ED_SHORTCUT("script_text_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD|KEY_L);
  1065. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_SHIFT|KEY_F1);
  1066. ScriptEditor::register_create_script_editor_function(create_editor);
  1067. }