code_editor.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*************************************************************************/
  2. /* code_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "code_editor.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/string_builder.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor_node.h"
  35. #include "editor_settings.h"
  36. #include "scene/gui/margin_container.h"
  37. #include "scene/gui/separator.h"
  38. #include "scene/resources/dynamic_font.h"
  39. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  40. text_editor = p_edit;
  41. line->set_text(itos(text_editor->cursor_get_line()));
  42. line->select_all();
  43. popup_centered(Size2(180, 80) * EDSCALE);
  44. line->grab_focus();
  45. }
  46. int GotoLineDialog::get_line() const {
  47. return line->get_text().to_int();
  48. }
  49. void GotoLineDialog::ok_pressed() {
  50. if (get_line() < 1 || get_line() > text_editor->get_line_count())
  51. return;
  52. text_editor->unfold_line(get_line() - 1);
  53. text_editor->cursor_set_line(get_line() - 1);
  54. hide();
  55. }
  56. GotoLineDialog::GotoLineDialog() {
  57. set_title(TTR("Go to Line"));
  58. VBoxContainer *vbc = memnew(VBoxContainer);
  59. vbc->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 8 * EDSCALE);
  60. vbc->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 8 * EDSCALE);
  61. vbc->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -8 * EDSCALE);
  62. vbc->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -8 * EDSCALE);
  63. add_child(vbc);
  64. Label *l = memnew(Label);
  65. l->set_text(TTR("Line Number:"));
  66. vbc->add_child(l);
  67. line = memnew(LineEdit);
  68. vbc->add_child(line);
  69. register_text_enter(line);
  70. text_editor = NULL;
  71. set_hide_on_ok(false);
  72. }
  73. void FindReplaceBar::_notification(int p_what) {
  74. if (p_what == NOTIFICATION_READY) {
  75. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  76. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  77. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  78. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  79. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  80. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  81. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  82. set_process_unhandled_input(is_visible_in_tree());
  83. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  84. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  85. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  86. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  87. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  88. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  89. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  90. }
  91. }
  92. void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) {
  93. Ref<InputEventKey> k = p_event;
  94. if (k.is_valid()) {
  95. if (k->is_pressed() && (text_edit->has_focus() || vbc_lineedit->is_a_parent_of(get_focus_owner()))) {
  96. bool accepted = true;
  97. switch (k->get_scancode()) {
  98. case KEY_ESCAPE: {
  99. _hide_bar();
  100. } break;
  101. default: {
  102. accepted = false;
  103. } break;
  104. }
  105. if (accepted) {
  106. accept_event();
  107. }
  108. }
  109. }
  110. }
  111. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  112. int line, col;
  113. String text = get_search_text();
  114. bool found = text_edit->search(text, p_flags, p_from_line, p_from_col, line, col);
  115. if (found) {
  116. if (!preserve_cursor) {
  117. text_edit->unfold_line(line);
  118. text_edit->cursor_set_line(line, false);
  119. text_edit->cursor_set_column(col + text.length(), false);
  120. text_edit->center_viewport_to_cursor();
  121. }
  122. text_edit->set_search_text(text);
  123. text_edit->set_search_flags(p_flags);
  124. text_edit->set_current_search_result(line, col);
  125. result_line = line;
  126. result_col = col;
  127. set_error("");
  128. } else {
  129. result_line = -1;
  130. result_col = -1;
  131. text_edit->set_search_text("");
  132. set_error(text.empty() ? "" : TTR("No Matches"));
  133. }
  134. return found;
  135. }
  136. void FindReplaceBar::_replace() {
  137. if (result_line != -1 && result_col != -1) {
  138. text_edit->begin_complex_operation();
  139. text_edit->unfold_line(result_line);
  140. text_edit->select(result_line, result_col, result_line, result_col + get_search_text().length());
  141. text_edit->insert_text_at_cursor(get_replace_text());
  142. text_edit->end_complex_operation();
  143. }
  144. search_current();
  145. }
  146. void FindReplaceBar::_replace_all() {
  147. text_edit->disconnect("text_changed", this, "_editor_text_changed");
  148. // line as x so it gets priority in comparison, column as y
  149. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
  150. Point2i prev_match = Point2(-1, -1);
  151. bool selection_enabled = text_edit->is_selection_active();
  152. Point2i selection_begin, selection_end;
  153. if (selection_enabled) {
  154. selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  155. selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  156. }
  157. int vsval = text_edit->get_v_scroll();
  158. text_edit->cursor_set_line(0);
  159. text_edit->cursor_set_column(0);
  160. String replace_text = get_replace_text();
  161. int search_text_len = get_search_text().length();
  162. int rc = 0;
  163. replace_all_mode = true;
  164. text_edit->begin_complex_operation();
  165. if (search_current()) {
  166. do {
  167. // replace area
  168. Point2i match_from(result_line, result_col);
  169. Point2i match_to(result_line, result_col + search_text_len);
  170. if (match_from < prev_match) {
  171. break; // done
  172. }
  173. prev_match = Point2i(result_line, result_col + replace_text.length());
  174. text_edit->unfold_line(result_line);
  175. text_edit->select(result_line, result_col, result_line, match_to.y);
  176. if (selection_enabled && is_selection_only()) {
  177. if (match_from < selection_begin || match_to > selection_end) {
  178. continue;
  179. }
  180. // replace but adjust selection bounds
  181. text_edit->insert_text_at_cursor(replace_text);
  182. if (match_to.x == selection_end.x) {
  183. selection_end.y += replace_text.length() - search_text_len;
  184. }
  185. } else {
  186. // just replace
  187. text_edit->insert_text_at_cursor(replace_text);
  188. }
  189. rc++;
  190. } while (search_next());
  191. }
  192. text_edit->end_complex_operation();
  193. replace_all_mode = false;
  194. // restore editor state (selection, cursor, scroll)
  195. text_edit->cursor_set_line(orig_cursor.x);
  196. text_edit->cursor_set_column(orig_cursor.y);
  197. if (selection_enabled && is_selection_only()) {
  198. // reselect
  199. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  200. } else {
  201. text_edit->deselect();
  202. }
  203. text_edit->set_v_scroll(vsval);
  204. set_error(vformat(TTR("Replaced %d occurrence(s)."), rc));
  205. text_edit->call_deferred("connect", "text_changed", this, "_editor_text_changed");
  206. }
  207. void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
  208. r_line = text_edit->cursor_get_line();
  209. r_col = text_edit->cursor_get_column();
  210. if (text_edit->is_selection_active() && !replace_all_mode) {
  211. int selection_line = text_edit->get_selection_from_line();
  212. if (text_edit->get_selection_text() == get_search_text() && r_line == selection_line) {
  213. int selection_from_col = text_edit->get_selection_from_column();
  214. if (r_col >= selection_from_col && r_col <= text_edit->get_selection_to_column()) {
  215. r_col = selection_from_col;
  216. }
  217. }
  218. }
  219. if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  220. r_col = result_col;
  221. }
  222. }
  223. bool FindReplaceBar::search_current() {
  224. uint32_t flags = 0;
  225. if (is_whole_words())
  226. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  227. if (is_case_sensitive())
  228. flags |= TextEdit::SEARCH_MATCH_CASE;
  229. int line, col;
  230. _get_search_from(line, col);
  231. return _search(flags, line, col);
  232. }
  233. bool FindReplaceBar::search_prev() {
  234. uint32_t flags = 0;
  235. String text = get_search_text();
  236. if (is_whole_words())
  237. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  238. if (is_case_sensitive())
  239. flags |= TextEdit::SEARCH_MATCH_CASE;
  240. flags |= TextEdit::SEARCH_BACKWARDS;
  241. int line, col;
  242. _get_search_from(line, col);
  243. col -= text.length();
  244. if (col < 0) {
  245. line -= 1;
  246. if (line < 0)
  247. line = text_edit->get_line_count() - 1;
  248. col = text_edit->get_line(line).length();
  249. }
  250. return _search(flags, line, col);
  251. }
  252. bool FindReplaceBar::search_next() {
  253. uint32_t flags = 0;
  254. String text = get_search_text();
  255. if (is_whole_words())
  256. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  257. if (is_case_sensitive())
  258. flags |= TextEdit::SEARCH_MATCH_CASE;
  259. int line, col;
  260. _get_search_from(line, col);
  261. if (line == result_line && col == result_col) {
  262. col += text.length();
  263. if (col > text_edit->get_line(line).length()) {
  264. line += 1;
  265. if (line >= text_edit->get_line_count())
  266. line = 0;
  267. col = 0;
  268. }
  269. }
  270. return _search(flags, line, col);
  271. }
  272. void FindReplaceBar::_hide_bar() {
  273. if (replace_text->has_focus() || search_text->has_focus())
  274. text_edit->grab_focus();
  275. text_edit->set_search_text("");
  276. result_line = -1;
  277. result_col = -1;
  278. set_error("");
  279. hide();
  280. }
  281. void FindReplaceBar::_show_search() {
  282. show();
  283. search_text->call_deferred("grab_focus");
  284. if (text_edit->is_selection_active() && !selection_only->is_pressed()) {
  285. search_text->set_text(text_edit->get_selection_text());
  286. }
  287. if (!get_search_text().empty()) {
  288. search_text->select_all();
  289. search_text->set_cursor_position(search_text->get_text().length());
  290. search_current();
  291. }
  292. }
  293. void FindReplaceBar::popup_search() {
  294. replace_text->hide();
  295. hbc_button_replace->hide();
  296. hbc_option_replace->hide();
  297. _show_search();
  298. }
  299. void FindReplaceBar::popup_replace() {
  300. if (!replace_text->is_visible_in_tree()) {
  301. replace_text->clear();
  302. replace_text->show();
  303. hbc_button_replace->show();
  304. hbc_option_replace->show();
  305. }
  306. selection_only->set_pressed((text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line()));
  307. _show_search();
  308. }
  309. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  310. search_current();
  311. }
  312. void FindReplaceBar::_editor_text_changed() {
  313. if (is_visible_in_tree()) {
  314. preserve_cursor = true;
  315. search_current();
  316. preserve_cursor = false;
  317. }
  318. }
  319. void FindReplaceBar::_search_text_changed(const String &p_text) {
  320. search_current();
  321. }
  322. void FindReplaceBar::_search_text_entered(const String &p_text) {
  323. search_next();
  324. }
  325. void FindReplaceBar::_replace_text_entered(const String &p_text) {
  326. if (selection_only->is_pressed() && text_edit->is_selection_active()) {
  327. _replace_all();
  328. _hide_bar();
  329. }
  330. }
  331. String FindReplaceBar::get_search_text() const {
  332. return search_text->get_text();
  333. }
  334. String FindReplaceBar::get_replace_text() const {
  335. return replace_text->get_text();
  336. }
  337. bool FindReplaceBar::is_case_sensitive() const {
  338. return case_sensitive->is_pressed();
  339. }
  340. bool FindReplaceBar::is_whole_words() const {
  341. return whole_words->is_pressed();
  342. }
  343. bool FindReplaceBar::is_selection_only() const {
  344. return selection_only->is_pressed();
  345. }
  346. void FindReplaceBar::set_error(const String &p_label) {
  347. emit_signal("error", p_label);
  348. }
  349. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  350. text_edit = p_text_edit;
  351. text_edit->connect("text_changed", this, "_editor_text_changed");
  352. }
  353. void FindReplaceBar::_bind_methods() {
  354. ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  355. ClassDB::bind_method("_editor_text_changed", &FindReplaceBar::_editor_text_changed);
  356. ClassDB::bind_method("_search_text_changed", &FindReplaceBar::_search_text_changed);
  357. ClassDB::bind_method("_search_text_entered", &FindReplaceBar::_search_text_entered);
  358. ClassDB::bind_method("_replace_text_entered", &FindReplaceBar::_replace_text_entered);
  359. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  360. ClassDB::bind_method("_search_next", &FindReplaceBar::search_next);
  361. ClassDB::bind_method("_search_prev", &FindReplaceBar::search_prev);
  362. ClassDB::bind_method("_replace_pressed", &FindReplaceBar::_replace);
  363. ClassDB::bind_method("_replace_all_pressed", &FindReplaceBar::_replace_all);
  364. ClassDB::bind_method("_search_options_changed", &FindReplaceBar::_search_options_changed);
  365. ClassDB::bind_method("_hide_pressed", &FindReplaceBar::_hide_bar);
  366. ADD_SIGNAL(MethodInfo("search"));
  367. ADD_SIGNAL(MethodInfo("error"));
  368. }
  369. FindReplaceBar::FindReplaceBar() {
  370. replace_all_mode = false;
  371. preserve_cursor = false;
  372. vbc_lineedit = memnew(VBoxContainer);
  373. add_child(vbc_lineedit);
  374. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  375. VBoxContainer *vbc_button = memnew(VBoxContainer);
  376. add_child(vbc_button);
  377. VBoxContainer *vbc_option = memnew(VBoxContainer);
  378. add_child(vbc_option);
  379. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  380. vbc_button->add_child(hbc_button_search);
  381. hbc_button_replace = memnew(HBoxContainer);
  382. vbc_button->add_child(hbc_button_replace);
  383. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  384. vbc_option->add_child(hbc_option_search);
  385. hbc_option_replace = memnew(HBoxContainer);
  386. vbc_option->add_child(hbc_option_replace);
  387. // search toolbar
  388. search_text = memnew(LineEdit);
  389. vbc_lineedit->add_child(search_text);
  390. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  391. search_text->connect("text_changed", this, "_search_text_changed");
  392. search_text->connect("text_entered", this, "_search_text_entered");
  393. find_prev = memnew(ToolButton);
  394. hbc_button_search->add_child(find_prev);
  395. find_prev->set_focus_mode(FOCUS_NONE);
  396. find_prev->connect("pressed", this, "_search_prev");
  397. find_next = memnew(ToolButton);
  398. hbc_button_search->add_child(find_next);
  399. find_next->set_focus_mode(FOCUS_NONE);
  400. find_next->connect("pressed", this, "_search_next");
  401. case_sensitive = memnew(CheckBox);
  402. hbc_option_search->add_child(case_sensitive);
  403. case_sensitive->set_text(TTR("Match Case"));
  404. case_sensitive->set_focus_mode(FOCUS_NONE);
  405. case_sensitive->connect("toggled", this, "_search_options_changed");
  406. whole_words = memnew(CheckBox);
  407. hbc_option_search->add_child(whole_words);
  408. whole_words->set_text(TTR("Whole Words"));
  409. whole_words->set_focus_mode(FOCUS_NONE);
  410. whole_words->connect("toggled", this, "_search_options_changed");
  411. // replace toolbar
  412. replace_text = memnew(LineEdit);
  413. vbc_lineedit->add_child(replace_text);
  414. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  415. replace_text->connect("text_entered", this, "_replace_text_entered");
  416. replace = memnew(Button);
  417. hbc_button_replace->add_child(replace);
  418. replace->set_text(TTR("Replace"));
  419. replace->connect("pressed", this, "_replace_pressed");
  420. replace_all = memnew(Button);
  421. hbc_button_replace->add_child(replace_all);
  422. replace_all->set_text(TTR("Replace All"));
  423. replace_all->connect("pressed", this, "_replace_all_pressed");
  424. selection_only = memnew(CheckBox);
  425. hbc_option_replace->add_child(selection_only);
  426. selection_only->set_text(TTR("Selection Only"));
  427. selection_only->set_focus_mode(FOCUS_NONE);
  428. selection_only->connect("toggled", this, "_search_options_changed");
  429. hide_button = memnew(TextureButton);
  430. add_child(hide_button);
  431. hide_button->set_focus_mode(FOCUS_NONE);
  432. hide_button->connect("pressed", this, "_hide_pressed");
  433. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  434. }
  435. /*** CODE EDITOR ****/
  436. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  437. Ref<InputEventMouseButton> mb = p_event;
  438. if (mb.is_valid()) {
  439. if (mb->is_pressed() && mb->get_command()) {
  440. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  441. _zoom_in();
  442. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  443. _zoom_out();
  444. }
  445. }
  446. }
  447. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  448. if (magnify_gesture.is_valid()) {
  449. Ref<DynamicFont> font = text_editor->get_font("font");
  450. if (font.is_valid()) {
  451. if (font->get_size() != (int)font_size) {
  452. font_size = font->get_size();
  453. }
  454. font_size *= powf(magnify_gesture->get_factor(), 0.25);
  455. _add_font_size((int)font_size - font->get_size());
  456. }
  457. return;
  458. }
  459. Ref<InputEventKey> k = p_event;
  460. if (k.is_valid()) {
  461. if (k->is_pressed()) {
  462. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  463. _zoom_in();
  464. }
  465. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  466. _zoom_out();
  467. }
  468. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  469. _reset_zoom();
  470. }
  471. }
  472. }
  473. }
  474. void CodeTextEditor::_zoom_in() {
  475. font_resize_val += MAX(EDSCALE, 1.0f);
  476. _zoom_changed();
  477. }
  478. void CodeTextEditor::_zoom_out() {
  479. font_resize_val -= MAX(EDSCALE, 1.0f);
  480. _zoom_changed();
  481. }
  482. void CodeTextEditor::_zoom_changed() {
  483. if (font_resize_timer->get_time_left() == 0)
  484. font_resize_timer->start();
  485. }
  486. void CodeTextEditor::_reset_zoom() {
  487. Ref<DynamicFont> font = text_editor->get_font("font"); // reset source font size to default
  488. if (font.is_valid()) {
  489. EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
  490. font->set_size(14);
  491. }
  492. }
  493. void CodeTextEditor::_line_col_changed() {
  494. String line = text_editor->get_line(text_editor->cursor_get_line());
  495. int positional_column = 0;
  496. for (int i = 0; i < text_editor->cursor_get_column(); i++) {
  497. if (line[i] == '\t') {
  498. positional_column += text_editor->get_indent_size(); //tab size
  499. } else {
  500. positional_column += 1;
  501. }
  502. }
  503. StringBuilder *sb = memnew(StringBuilder);
  504. sb->append("(");
  505. sb->append(itos(text_editor->cursor_get_line() + 1).lpad(3));
  506. sb->append(",");
  507. sb->append(itos(positional_column + 1).lpad(3));
  508. sb->append(")");
  509. line_and_col_txt->set_text(sb->as_string());
  510. }
  511. void CodeTextEditor::_text_changed() {
  512. if (text_editor->is_insert_text_operation()) {
  513. code_complete_timer->start();
  514. }
  515. idle->start();
  516. }
  517. void CodeTextEditor::_code_complete_timer_timeout() {
  518. if (!is_visible_in_tree())
  519. return;
  520. text_editor->query_code_comple();
  521. }
  522. void CodeTextEditor::_complete_request() {
  523. List<String> entries;
  524. String ctext = text_editor->get_text_for_completion();
  525. _code_complete_script(ctext, &entries);
  526. bool forced = false;
  527. if (code_complete_func) {
  528. code_complete_func(code_complete_ud, ctext, &entries, forced);
  529. }
  530. if (entries.size() == 0)
  531. return;
  532. Vector<String> strs;
  533. strs.resize(entries.size());
  534. int i = 0;
  535. for (List<String>::Element *E = entries.front(); E; E = E->next()) {
  536. strs.write[i++] = E->get();
  537. }
  538. text_editor->code_complete(strs, forced);
  539. }
  540. void CodeTextEditor::_font_resize_timeout() {
  541. if (_add_font_size(font_resize_val)) {
  542. font_resize_val = 0;
  543. }
  544. }
  545. bool CodeTextEditor::_add_font_size(int p_delta) {
  546. Ref<DynamicFont> font = text_editor->get_font("font");
  547. if (font.is_valid()) {
  548. int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE);
  549. if (new_size != font->get_size()) {
  550. EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE);
  551. font->set_size(new_size);
  552. }
  553. return true;
  554. } else {
  555. return false;
  556. }
  557. }
  558. void CodeTextEditor::update_editor_settings() {
  559. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  560. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  561. text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  562. text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  563. text_editor->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  564. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  565. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers"));
  566. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_numbers_zero_padded"));
  567. text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_length_guideline"));
  568. text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_length_guideline_column"));
  569. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  570. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  571. text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  572. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  573. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  574. text_editor->set_breakpoint_gutter_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_breakpoint_gutter"));
  575. text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
  576. text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
  577. text_editor->set_wrap_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/word_wrap"));
  578. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  579. text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/open_scripts/smooth_scrolling"));
  580. text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/open_scripts/v_scroll_speed"));
  581. }
  582. void CodeTextEditor::trim_trailing_whitespace() {
  583. bool trimed_whitespace = false;
  584. for (int i = 0; i < text_editor->get_line_count(); i++) {
  585. String line = text_editor->get_line(i);
  586. if (line.ends_with(" ") || line.ends_with("\t")) {
  587. if (!trimed_whitespace) {
  588. text_editor->begin_complex_operation();
  589. trimed_whitespace = true;
  590. }
  591. int end = 0;
  592. for (int j = line.length() - 1; j > -1; j--) {
  593. if (line[j] != ' ' && line[j] != '\t') {
  594. end = j + 1;
  595. break;
  596. }
  597. }
  598. text_editor->set_line(i, line.substr(0, end));
  599. }
  600. }
  601. if (trimed_whitespace) {
  602. text_editor->end_complex_operation();
  603. text_editor->update();
  604. }
  605. }
  606. void CodeTextEditor::convert_indent_to_spaces() {
  607. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  608. String indent = "";
  609. for (int i = 0; i < indent_size; i++) {
  610. indent += " ";
  611. }
  612. int cursor_line = text_editor->cursor_get_line();
  613. int cursor_column = text_editor->cursor_get_column();
  614. bool changed_indentation = false;
  615. for (int i = 0; i < text_editor->get_line_count(); i++) {
  616. String line = text_editor->get_line(i);
  617. if (line.length() <= 0) {
  618. continue;
  619. }
  620. int j = 0;
  621. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  622. if (line[j] == '\t') {
  623. if (!changed_indentation) {
  624. text_editor->begin_complex_operation();
  625. changed_indentation = true;
  626. }
  627. if (cursor_line == i && cursor_column > j) {
  628. cursor_column += indent_size - 1;
  629. }
  630. line = line.left(j) + indent + line.right(j + 1);
  631. }
  632. j++;
  633. }
  634. if (changed_indentation) {
  635. text_editor->set_line(i, line);
  636. }
  637. }
  638. if (changed_indentation) {
  639. text_editor->cursor_set_column(cursor_column);
  640. text_editor->end_complex_operation();
  641. text_editor->update();
  642. }
  643. }
  644. void CodeTextEditor::convert_indent_to_tabs() {
  645. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  646. indent_size -= 1;
  647. int cursor_line = text_editor->cursor_get_line();
  648. int cursor_column = text_editor->cursor_get_column();
  649. bool changed_indentation = false;
  650. for (int i = 0; i < text_editor->get_line_count(); i++) {
  651. String line = text_editor->get_line(i);
  652. if (line.length() <= 0) {
  653. continue;
  654. }
  655. int j = 0;
  656. int space_count = -1;
  657. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  658. if (line[j] != '\t') {
  659. space_count++;
  660. if (space_count == indent_size) {
  661. if (!changed_indentation) {
  662. text_editor->begin_complex_operation();
  663. changed_indentation = true;
  664. }
  665. if (cursor_line == i && cursor_column > j) {
  666. cursor_column -= indent_size;
  667. }
  668. line = line.left(j - indent_size) + "\t" + line.right(j + 1);
  669. j = 0;
  670. space_count = -1;
  671. }
  672. } else {
  673. space_count = -1;
  674. }
  675. j++;
  676. }
  677. if (changed_indentation) {
  678. text_editor->set_line(i, line);
  679. }
  680. }
  681. if (changed_indentation) {
  682. text_editor->cursor_set_column(cursor_column);
  683. text_editor->end_complex_operation();
  684. text_editor->update();
  685. }
  686. }
  687. void CodeTextEditor::convert_case(CaseStyle p_case) {
  688. if (!text_editor->is_selection_active()) {
  689. return;
  690. }
  691. text_editor->begin_complex_operation();
  692. int begin = text_editor->get_selection_from_line();
  693. int end = text_editor->get_selection_to_line();
  694. int begin_col = text_editor->get_selection_from_column();
  695. int end_col = text_editor->get_selection_to_column();
  696. for (int i = begin; i <= end; i++) {
  697. int len = text_editor->get_line(i).length();
  698. if (i == end)
  699. len -= len - end_col;
  700. if (i == begin)
  701. len -= begin_col;
  702. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  703. switch (p_case) {
  704. case UPPER: {
  705. new_line = new_line.to_upper();
  706. } break;
  707. case LOWER: {
  708. new_line = new_line.to_lower();
  709. } break;
  710. case CAPITALIZE: {
  711. new_line = new_line.capitalize();
  712. } break;
  713. }
  714. if (i == begin) {
  715. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  716. }
  717. if (i == end) {
  718. new_line = new_line + text_editor->get_line(i).right(end_col);
  719. }
  720. text_editor->set_line(i, new_line);
  721. }
  722. text_editor->end_complex_operation();
  723. }
  724. void CodeTextEditor::move_lines_up() {
  725. text_editor->begin_complex_operation();
  726. if (text_editor->is_selection_active()) {
  727. int from_line = text_editor->get_selection_from_line();
  728. int from_col = text_editor->get_selection_from_column();
  729. int to_line = text_editor->get_selection_to_line();
  730. int to_column = text_editor->get_selection_to_column();
  731. for (int i = from_line; i <= to_line; i++) {
  732. int line_id = i;
  733. int next_id = i - 1;
  734. if (line_id == 0 || next_id < 0)
  735. return;
  736. text_editor->unfold_line(line_id);
  737. text_editor->unfold_line(next_id);
  738. text_editor->swap_lines(line_id, next_id);
  739. text_editor->cursor_set_line(next_id);
  740. }
  741. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  742. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  743. text_editor->select(from_line_up, from_col, to_line_up, to_column);
  744. } else {
  745. int line_id = text_editor->cursor_get_line();
  746. int next_id = line_id - 1;
  747. if (line_id == 0 || next_id < 0)
  748. return;
  749. text_editor->unfold_line(line_id);
  750. text_editor->unfold_line(next_id);
  751. text_editor->swap_lines(line_id, next_id);
  752. text_editor->cursor_set_line(next_id);
  753. }
  754. text_editor->end_complex_operation();
  755. text_editor->update();
  756. }
  757. void CodeTextEditor::move_lines_down() {
  758. text_editor->begin_complex_operation();
  759. if (text_editor->is_selection_active()) {
  760. int from_line = text_editor->get_selection_from_line();
  761. int from_col = text_editor->get_selection_from_column();
  762. int to_line = text_editor->get_selection_to_line();
  763. int to_column = text_editor->get_selection_to_column();
  764. for (int i = to_line; i >= from_line; i--) {
  765. int line_id = i;
  766. int next_id = i + 1;
  767. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count())
  768. return;
  769. text_editor->unfold_line(line_id);
  770. text_editor->unfold_line(next_id);
  771. text_editor->swap_lines(line_id, next_id);
  772. text_editor->cursor_set_line(next_id);
  773. }
  774. int from_line_down = from_line < text_editor->get_line_count() ? from_line + 1 : from_line;
  775. int to_line_down = to_line < text_editor->get_line_count() ? to_line + 1 : to_line;
  776. text_editor->select(from_line_down, from_col, to_line_down, to_column);
  777. } else {
  778. int line_id = text_editor->cursor_get_line();
  779. int next_id = line_id + 1;
  780. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count())
  781. return;
  782. text_editor->unfold_line(line_id);
  783. text_editor->unfold_line(next_id);
  784. text_editor->swap_lines(line_id, next_id);
  785. text_editor->cursor_set_line(next_id);
  786. }
  787. text_editor->end_complex_operation();
  788. text_editor->update();
  789. }
  790. void CodeTextEditor::delete_lines() {
  791. text_editor->begin_complex_operation();
  792. if (text_editor->is_selection_active()) {
  793. int to_line = text_editor->get_selection_to_line();
  794. int from_line = text_editor->get_selection_from_line();
  795. int count = Math::abs(to_line - from_line) + 1;
  796. text_editor->cursor_set_line(to_line, false);
  797. while (count) {
  798. text_editor->set_line(text_editor->cursor_get_line(), "");
  799. text_editor->backspace_at_cursor();
  800. count--;
  801. if (count)
  802. text_editor->unfold_line(from_line);
  803. }
  804. text_editor->cursor_set_line(from_line - 1);
  805. text_editor->deselect();
  806. } else {
  807. int line = text_editor->cursor_get_line();
  808. text_editor->set_line(text_editor->cursor_get_line(), "");
  809. text_editor->backspace_at_cursor();
  810. text_editor->unfold_line(line);
  811. text_editor->cursor_set_line(line);
  812. }
  813. text_editor->end_complex_operation();
  814. }
  815. void CodeTextEditor::clone_lines_down() {
  816. int from_line = text_editor->cursor_get_line();
  817. int to_line = text_editor->cursor_get_line();
  818. int column = text_editor->cursor_get_column();
  819. if (text_editor->is_selection_active()) {
  820. from_line = text_editor->get_selection_from_line();
  821. to_line = text_editor->get_selection_to_line();
  822. column = text_editor->cursor_get_column();
  823. }
  824. int next_line = to_line + 1;
  825. bool caret_at_start = text_editor->cursor_get_line() == from_line;
  826. text_editor->begin_complex_operation();
  827. for (int i = from_line; i <= to_line; i++) {
  828. text_editor->unfold_line(i);
  829. text_editor->set_line(next_line - 1, text_editor->get_line(next_line - 1) + "\n");
  830. text_editor->set_line(next_line, text_editor->get_line(i));
  831. next_line++;
  832. }
  833. if (caret_at_start) {
  834. text_editor->cursor_set_line(to_line + 1);
  835. } else {
  836. text_editor->cursor_set_line(next_line - 1);
  837. }
  838. text_editor->cursor_set_column(column);
  839. if (text_editor->is_selection_active()) {
  840. text_editor->select(to_line + 1, text_editor->get_selection_from_column(), next_line - 1, text_editor->get_selection_to_column());
  841. }
  842. text_editor->end_complex_operation();
  843. text_editor->update();
  844. }
  845. void CodeTextEditor::goto_line(int p_line) {
  846. text_editor->deselect();
  847. text_editor->unfold_line(p_line);
  848. text_editor->call_deferred("cursor_set_line", p_line);
  849. }
  850. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  851. text_editor->unfold_line(p_line);
  852. text_editor->call_deferred("cursor_set_line", p_line);
  853. text_editor->call_deferred("cursor_set_column", p_begin);
  854. text_editor->select(p_line, p_begin, p_line, p_end);
  855. }
  856. Variant CodeTextEditor::get_edit_state() {
  857. Dictionary state;
  858. state["scroll_position"] = text_editor->get_v_scroll();
  859. state["column"] = text_editor->cursor_get_column();
  860. state["row"] = text_editor->cursor_get_line();
  861. return state;
  862. }
  863. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  864. Dictionary state = p_state;
  865. text_editor->cursor_set_column(state["column"]);
  866. text_editor->cursor_set_line(state["row"]);
  867. text_editor->set_v_scroll(state["scroll_position"]);
  868. text_editor->grab_focus();
  869. }
  870. void CodeTextEditor::set_error(const String &p_error) {
  871. error->set_text(p_error);
  872. if (p_error != "") {
  873. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  874. } else {
  875. error->set_default_cursor_shape(CURSOR_ARROW);
  876. }
  877. }
  878. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  879. error_line = p_line;
  880. error_column = p_column;
  881. }
  882. void CodeTextEditor::goto_error() {
  883. if (error->get_text() != "") {
  884. text_editor->cursor_set_line(error_line);
  885. text_editor->cursor_set_column(error_column);
  886. text_editor->center_viewport_to_cursor();
  887. }
  888. }
  889. void CodeTextEditor::_update_font() {
  890. text_editor->add_font_override("font", get_font("source", "EditorFonts"));
  891. Ref<Font> status_bar_font = get_font("status_source", "EditorFonts");
  892. int count = status_bar->get_child_count();
  893. for (int i = 0; i < count; i++) {
  894. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  895. if (n)
  896. n->add_font_override("font", status_bar_font);
  897. }
  898. }
  899. void CodeTextEditor::_on_settings_change() {
  900. _update_font();
  901. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  902. // AUTO BRACE COMPLETION
  903. text_editor->set_auto_brace_completion(
  904. EDITOR_DEF("text_editor/completion/auto_brace_complete", true));
  905. code_complete_timer->set_wait_time(
  906. EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  907. // call hint settings
  908. text_editor->set_callhint_settings(
  909. EDITOR_DEF("text_editor/completion/put_callhint_tooltip_below_current_line", true),
  910. EDITOR_DEF("text_editor/completion/callhint_tooltip_offset", Vector2()));
  911. }
  912. void CodeTextEditor::_text_changed_idle_timeout() {
  913. _validate_script();
  914. emit_signal("validate_script");
  915. }
  916. void CodeTextEditor::_warning_label_gui_input(const Ref<InputEvent> &p_event) {
  917. Ref<InputEventMouseButton> mb = p_event;
  918. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  919. _warning_button_pressed();
  920. }
  921. }
  922. void CodeTextEditor::_warning_button_pressed() {
  923. emit_signal("warning_pressed");
  924. }
  925. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  926. Ref<InputEventMouseButton> mb = p_event;
  927. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  928. emit_signal("error_pressed");
  929. }
  930. }
  931. void CodeTextEditor::_notification(int p_what) {
  932. switch (p_what) {
  933. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  934. _load_theme_settings();
  935. emit_signal("load_theme_settings");
  936. } break;
  937. case NOTIFICATION_THEME_CHANGED: {
  938. _update_font();
  939. } break;
  940. case NOTIFICATION_ENTER_TREE: {
  941. warning_button->set_icon(get_icon("NodeWarning", "EditorIcons"));
  942. add_constant_override("separation", 4 * EDSCALE);
  943. } break;
  944. default:
  945. break;
  946. }
  947. }
  948. void CodeTextEditor::set_warning_nb(int p_warning_nb) {
  949. warning_count_label->set_text(itos(p_warning_nb));
  950. warning_count_label->set_visible(p_warning_nb > 0);
  951. warning_button->set_visible(p_warning_nb > 0);
  952. }
  953. void CodeTextEditor::_bind_methods() {
  954. ClassDB::bind_method("_text_editor_gui_input", &CodeTextEditor::_text_editor_gui_input);
  955. ClassDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed);
  956. ClassDB::bind_method("_text_changed", &CodeTextEditor::_text_changed);
  957. ClassDB::bind_method("_on_settings_change", &CodeTextEditor::_on_settings_change);
  958. ClassDB::bind_method("_text_changed_idle_timeout", &CodeTextEditor::_text_changed_idle_timeout);
  959. ClassDB::bind_method("_code_complete_timer_timeout", &CodeTextEditor::_code_complete_timer_timeout);
  960. ClassDB::bind_method("_complete_request", &CodeTextEditor::_complete_request);
  961. ClassDB::bind_method("_font_resize_timeout", &CodeTextEditor::_font_resize_timeout);
  962. ClassDB::bind_method("_error_pressed", &CodeTextEditor::_error_pressed);
  963. ClassDB::bind_method("_warning_button_pressed", &CodeTextEditor::_warning_button_pressed);
  964. ClassDB::bind_method("_warning_label_gui_input", &CodeTextEditor::_warning_label_gui_input);
  965. ADD_SIGNAL(MethodInfo("validate_script"));
  966. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  967. ADD_SIGNAL(MethodInfo("warning_pressed"));
  968. ADD_SIGNAL(MethodInfo("error_pressed"));
  969. }
  970. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  971. code_complete_func = p_code_complete_func;
  972. code_complete_ud = p_ud;
  973. }
  974. CodeTextEditor::CodeTextEditor() {
  975. code_complete_func = NULL;
  976. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  977. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  978. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  979. text_editor = memnew(TextEdit);
  980. add_child(text_editor);
  981. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  982. // Added second so it opens at the bottom, so it won't shift the entire text editor when opening.
  983. find_replace_bar = memnew(FindReplaceBar);
  984. add_child(find_replace_bar);
  985. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  986. find_replace_bar->hide();
  987. find_replace_bar->set_text_edit(text_editor);
  988. text_editor->set_show_line_numbers(true);
  989. text_editor->set_brace_matching(true);
  990. text_editor->set_auto_indent(true);
  991. status_bar = memnew(HBoxContainer);
  992. add_child(status_bar);
  993. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  994. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  995. idle = memnew(Timer);
  996. add_child(idle);
  997. idle->set_one_shot(true);
  998. idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2));
  999. code_complete_timer = memnew(Timer);
  1000. add_child(code_complete_timer);
  1001. code_complete_timer->set_one_shot(true);
  1002. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  1003. error_line = 0;
  1004. error_column = 0;
  1005. // Error
  1006. ScrollContainer *scroll = memnew(ScrollContainer);
  1007. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1008. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1009. scroll->set_enable_v_scroll(false);
  1010. status_bar->add_child(scroll);
  1011. error = memnew(Label);
  1012. scroll->add_child(error);
  1013. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1014. error->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
  1015. error->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1016. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1017. error->connect("gui_input", this, "_error_pressed");
  1018. find_replace_bar->connect("error", error, "set_text");
  1019. // Warnings
  1020. warning_button = memnew(ToolButton);
  1021. status_bar->add_child(warning_button);
  1022. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1023. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1024. warning_button->connect("pressed", this, "_warning_button_pressed");
  1025. warning_button->set_tooltip(TTR("Warnings"));
  1026. warning_count_label = memnew(Label);
  1027. status_bar->add_child(warning_count_label);
  1028. warning_count_label->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1029. warning_count_label->set_align(Label::ALIGN_RIGHT);
  1030. warning_count_label->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1031. warning_count_label->set_mouse_filter(MOUSE_FILTER_STOP);
  1032. warning_count_label->set_tooltip(TTR("Warnings"));
  1033. warning_count_label->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("warning_color", "Editor"));
  1034. warning_count_label->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1035. warning_count_label->connect("gui_input", this, "_warning_label_gui_input");
  1036. set_warning_nb(0);
  1037. // Line and column
  1038. line_and_col_txt = memnew(Label);
  1039. status_bar->add_child(line_and_col_txt);
  1040. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1041. line_and_col_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1042. line_and_col_txt->set_tooltip(TTR("Line and column numbers."));
  1043. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1044. text_editor->connect("gui_input", this, "_text_editor_gui_input");
  1045. text_editor->connect("cursor_changed", this, "_line_col_changed");
  1046. text_editor->connect("text_changed", this, "_text_changed");
  1047. text_editor->connect("request_completion", this, "_complete_request");
  1048. Vector<String> cs;
  1049. cs.push_back(".");
  1050. cs.push_back(",");
  1051. cs.push_back("(");
  1052. cs.push_back("=");
  1053. cs.push_back("$");
  1054. text_editor->set_completion(true, cs);
  1055. idle->connect("timeout", this, "_text_changed_idle_timeout");
  1056. code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");
  1057. font_resize_val = 0;
  1058. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  1059. font_resize_timer = memnew(Timer);
  1060. add_child(font_resize_timer);
  1061. font_resize_timer->set_one_shot(true);
  1062. font_resize_timer->set_wait_time(0.07);
  1063. font_resize_timer->connect("timeout", this, "_font_resize_timeout");
  1064. EditorSettings::get_singleton()->connect("settings_changed", this, "_on_settings_change");
  1065. }