code_editor.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*************************************************************************/
  2. /* code_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "editor/editor_scale.h"
  32. #include "editor_node.h"
  33. #include "editor_settings.h"
  34. #include "os/keyboard.h"
  35. #include "scene/gui/margin_container.h"
  36. #include "scene/gui/separator.h"
  37. #include "scene/resources/dynamic_font.h"
  38. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  39. text_editor = p_edit;
  40. line->set_text(itos(text_editor->cursor_get_line()));
  41. line->select_all();
  42. popup_centered(Size2(180, 80));
  43. line->grab_focus();
  44. }
  45. int GotoLineDialog::get_line() const {
  46. return line->get_text().to_int();
  47. }
  48. void GotoLineDialog::ok_pressed() {
  49. if (get_line() < 1 || get_line() > text_editor->get_line_count())
  50. return;
  51. text_editor->unfold_line(get_line() - 1);
  52. text_editor->cursor_set_line(get_line() - 1);
  53. hide();
  54. }
  55. GotoLineDialog::GotoLineDialog() {
  56. set_title(TTR("Go to Line"));
  57. Label *l = memnew(Label);
  58. l->set_text(TTR("Line Number:"));
  59. l->set_position(Point2(5, 5));
  60. add_child(l);
  61. line = memnew(LineEdit);
  62. line->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  63. line->set_begin(Point2(15, 22));
  64. line->set_end(Point2(-15, 35));
  65. add_child(line);
  66. register_text_enter(line);
  67. text_editor = NULL;
  68. set_hide_on_ok(false);
  69. }
  70. void FindReplaceBar::_notification(int p_what) {
  71. if (p_what == NOTIFICATION_READY) {
  72. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  73. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  74. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  75. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  76. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  77. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  78. set_process_unhandled_input(is_visible_in_tree());
  79. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  80. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  81. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  82. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  83. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  84. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  85. }
  86. }
  87. void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) {
  88. Ref<InputEventKey> k = p_event;
  89. if (k.is_valid()) {
  90. if (k->is_pressed() && (text_edit->has_focus() || text_vbc->is_a_parent_of(get_focus_owner()))) {
  91. bool accepted = true;
  92. switch (k->get_scancode()) {
  93. case KEY_ESCAPE: {
  94. _hide_bar();
  95. } break;
  96. default: {
  97. accepted = false;
  98. } break;
  99. }
  100. if (accepted) {
  101. accept_event();
  102. }
  103. }
  104. }
  105. }
  106. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  107. int line, col;
  108. String text = get_search_text();
  109. bool found = text_edit->search(text, p_flags, p_from_line, p_from_col, line, col);
  110. if (found) {
  111. if (!preserve_cursor) {
  112. text_edit->unfold_line(line);
  113. text_edit->cursor_set_line(line, false);
  114. text_edit->cursor_set_column(col + text.length(), false);
  115. text_edit->center_viewport_to_cursor();
  116. }
  117. text_edit->set_search_text(text);
  118. text_edit->set_search_flags(p_flags);
  119. text_edit->set_current_search_result(line, col);
  120. result_line = line;
  121. result_col = col;
  122. set_error("");
  123. } else {
  124. result_line = -1;
  125. result_col = -1;
  126. text_edit->set_search_text("");
  127. set_error(text.empty() ? "" : TTR("No Matches"));
  128. }
  129. return found;
  130. }
  131. void FindReplaceBar::_replace() {
  132. if (result_line != -1 && result_col != -1) {
  133. text_edit->begin_complex_operation();
  134. text_edit->unfold_line(result_line);
  135. text_edit->select(result_line, result_col, result_line, result_col + get_search_text().length());
  136. text_edit->insert_text_at_cursor(get_replace_text());
  137. text_edit->end_complex_operation();
  138. }
  139. search_current();
  140. }
  141. void FindReplaceBar::_replace_all() {
  142. // line as x so it gets priority in comparison, column as y
  143. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
  144. Point2i prev_match = Point2(-1, -1);
  145. bool selection_enabled = text_edit->is_selection_active();
  146. Point2i selection_begin, selection_end;
  147. if (selection_enabled) {
  148. selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  149. selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  150. }
  151. int vsval = text_edit->get_v_scroll();
  152. text_edit->cursor_set_line(0);
  153. text_edit->cursor_set_column(0);
  154. String replace_text = get_replace_text();
  155. int search_text_len = get_search_text().length();
  156. int rc = 0;
  157. replace_all_mode = true;
  158. text_edit->begin_complex_operation();
  159. while (search_next()) {
  160. // replace area
  161. Point2i match_from(result_line, result_col);
  162. Point2i match_to(result_line, result_col + search_text_len);
  163. if (match_from < prev_match)
  164. break; // done
  165. prev_match = Point2i(result_line, result_col + replace_text.length());
  166. text_edit->unfold_line(result_line);
  167. text_edit->select(result_line, result_col, result_line, match_to.y);
  168. if (selection_enabled && is_selection_only()) {
  169. if (match_from < selection_begin || match_to > selection_end)
  170. continue;
  171. // replace but adjust selection bounds
  172. text_edit->insert_text_at_cursor(replace_text);
  173. if (match_to.x == selection_end.x)
  174. selection_end.y += replace_text.length() - search_text_len;
  175. } else {
  176. // just replace
  177. text_edit->insert_text_at_cursor(replace_text);
  178. }
  179. rc++;
  180. }
  181. text_edit->end_complex_operation();
  182. replace_all_mode = false;
  183. // restore editor state (selection, cursor, scroll)
  184. text_edit->cursor_set_line(orig_cursor.x);
  185. text_edit->cursor_set_column(orig_cursor.y);
  186. if (selection_enabled && is_selection_only()) {
  187. // reselect
  188. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  189. } else {
  190. text_edit->deselect();
  191. }
  192. text_edit->set_v_scroll(vsval);
  193. set_error(vformat(TTR("Replaced %d occurrence(s)."), rc));
  194. }
  195. void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
  196. r_line = text_edit->cursor_get_line();
  197. r_col = text_edit->cursor_get_column();
  198. if (text_edit->is_selection_active() && !replace_all_mode) {
  199. int selection_line = text_edit->get_selection_from_line();
  200. if (text_edit->get_selection_text() == get_search_text() && r_line == selection_line) {
  201. int selection_from_col = text_edit->get_selection_from_column();
  202. if (r_col >= selection_from_col && r_col <= text_edit->get_selection_to_column()) {
  203. r_col = selection_from_col;
  204. }
  205. }
  206. }
  207. if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  208. r_col = result_col;
  209. }
  210. }
  211. bool FindReplaceBar::search_current() {
  212. uint32_t flags = 0;
  213. if (is_whole_words())
  214. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  215. if (is_case_sensitive())
  216. flags |= TextEdit::SEARCH_MATCH_CASE;
  217. int line, col;
  218. _get_search_from(line, col);
  219. return _search(flags, line, col);
  220. }
  221. bool FindReplaceBar::search_prev() {
  222. uint32_t flags = 0;
  223. String text = get_search_text();
  224. if (is_whole_words())
  225. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  226. if (is_case_sensitive())
  227. flags |= TextEdit::SEARCH_MATCH_CASE;
  228. flags |= TextEdit::SEARCH_BACKWARDS;
  229. int line, col;
  230. _get_search_from(line, col);
  231. col -= text.length();
  232. if (col < 0) {
  233. line -= 1;
  234. if (line < 0)
  235. line = text_edit->get_line_count() - 1;
  236. col = text_edit->get_line(line).length();
  237. }
  238. return _search(flags, line, col);
  239. }
  240. bool FindReplaceBar::search_next() {
  241. uint32_t flags = 0;
  242. String text = get_search_text();
  243. if (is_whole_words())
  244. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  245. if (is_case_sensitive())
  246. flags |= TextEdit::SEARCH_MATCH_CASE;
  247. int line, col;
  248. _get_search_from(line, col);
  249. if (line == result_line && col == result_col) {
  250. col += text.length();
  251. if (col > text_edit->get_line(line).length()) {
  252. line += 1;
  253. if (line >= text_edit->get_line_count())
  254. line = 0;
  255. col = 0;
  256. }
  257. }
  258. return _search(flags, line, col);
  259. }
  260. void FindReplaceBar::_hide_bar() {
  261. if (replace_text->has_focus() || search_text->has_focus())
  262. text_edit->grab_focus();
  263. text_edit->set_search_text("");
  264. result_line = -1;
  265. result_col = -1;
  266. replace_hbc->hide();
  267. replace_options_hbc->hide();
  268. hide();
  269. }
  270. void FindReplaceBar::_show_search() {
  271. show();
  272. search_text->grab_focus();
  273. if (text_edit->is_selection_active() && !selection_only->is_pressed()) {
  274. search_text->set_text(text_edit->get_selection_text());
  275. }
  276. if (!get_search_text().empty()) {
  277. search_text->select_all();
  278. search_text->set_cursor_position(search_text->get_text().length());
  279. search_current();
  280. }
  281. }
  282. void FindReplaceBar::popup_search() {
  283. replace_hbc->hide();
  284. replace_options_hbc->hide();
  285. _show_search();
  286. }
  287. void FindReplaceBar::popup_replace() {
  288. if (!replace_hbc->is_visible_in_tree() || !replace_options_hbc->is_visible_in_tree()) {
  289. replace_text->clear();
  290. replace_hbc->show();
  291. replace_options_hbc->show();
  292. }
  293. selection_only->set_pressed((text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line()));
  294. _show_search();
  295. }
  296. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  297. search_current();
  298. }
  299. void FindReplaceBar::_editor_text_changed() {
  300. if (is_visible_in_tree()) {
  301. preserve_cursor = true;
  302. search_current();
  303. preserve_cursor = false;
  304. }
  305. }
  306. void FindReplaceBar::_search_text_changed(const String &p_text) {
  307. search_current();
  308. }
  309. void FindReplaceBar::_search_text_entered(const String &p_text) {
  310. search_next();
  311. }
  312. void FindReplaceBar::_replace_text_entered(const String &p_text) {
  313. if (selection_only->is_pressed() && text_edit->is_selection_active()) {
  314. _replace_all();
  315. _hide_bar();
  316. }
  317. }
  318. String FindReplaceBar::get_search_text() const {
  319. return search_text->get_text();
  320. }
  321. String FindReplaceBar::get_replace_text() const {
  322. return replace_text->get_text();
  323. }
  324. bool FindReplaceBar::is_case_sensitive() const {
  325. return case_sensitive->is_pressed();
  326. }
  327. bool FindReplaceBar::is_whole_words() const {
  328. return whole_words->is_pressed();
  329. }
  330. bool FindReplaceBar::is_selection_only() const {
  331. return selection_only->is_pressed();
  332. }
  333. void FindReplaceBar::set_error(const String &p_label) {
  334. error_label->set_text(p_label);
  335. }
  336. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  337. text_edit = p_text_edit;
  338. text_edit->connect("text_changed", this, "_editor_text_changed");
  339. }
  340. void FindReplaceBar::_bind_methods() {
  341. ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  342. ClassDB::bind_method("_editor_text_changed", &FindReplaceBar::_editor_text_changed);
  343. ClassDB::bind_method("_search_text_changed", &FindReplaceBar::_search_text_changed);
  344. ClassDB::bind_method("_search_text_entered", &FindReplaceBar::_search_text_entered);
  345. ClassDB::bind_method("_replace_text_entered", &FindReplaceBar::_replace_text_entered);
  346. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  347. ClassDB::bind_method("_search_next", &FindReplaceBar::search_next);
  348. ClassDB::bind_method("_search_prev", &FindReplaceBar::search_prev);
  349. ClassDB::bind_method("_replace_pressed", &FindReplaceBar::_replace);
  350. ClassDB::bind_method("_replace_all_pressed", &FindReplaceBar::_replace_all);
  351. ClassDB::bind_method("_search_options_changed", &FindReplaceBar::_search_options_changed);
  352. ClassDB::bind_method("_hide_pressed", &FindReplaceBar::_hide_bar);
  353. ADD_SIGNAL(MethodInfo("search"));
  354. }
  355. FindReplaceBar::FindReplaceBar() {
  356. replace_all_mode = false;
  357. preserve_cursor = false;
  358. text_vbc = memnew(VBoxContainer);
  359. add_child(text_vbc);
  360. HBoxContainer *search_hbc = memnew(HBoxContainer);
  361. text_vbc->add_child(search_hbc);
  362. search_text = memnew(LineEdit);
  363. search_hbc->add_child(search_text);
  364. search_text->set_custom_minimum_size(Size2(200, 0));
  365. search_text->connect("text_changed", this, "_search_text_changed");
  366. search_text->connect("text_entered", this, "_search_text_entered");
  367. find_prev = memnew(ToolButton);
  368. search_hbc->add_child(find_prev);
  369. find_prev->set_focus_mode(FOCUS_NONE);
  370. find_prev->connect("pressed", this, "_search_prev");
  371. find_next = memnew(ToolButton);
  372. search_hbc->add_child(find_next);
  373. find_next->set_focus_mode(FOCUS_NONE);
  374. find_next->connect("pressed", this, "_search_next");
  375. replace_hbc = memnew(HBoxContainer);
  376. text_vbc->add_child(replace_hbc);
  377. replace_hbc->hide();
  378. replace_text = memnew(LineEdit);
  379. replace_hbc->add_child(replace_text);
  380. replace_text->set_custom_minimum_size(Size2(200, 0));
  381. replace_text->connect("text_entered", this, "_replace_text_entered");
  382. replace = memnew(Button);
  383. replace_hbc->add_child(replace);
  384. replace->set_text(TTR("Replace"));
  385. //replace->set_focus_mode(FOCUS_NONE);
  386. replace->connect("pressed", this, "_replace_pressed");
  387. replace_all = memnew(Button);
  388. replace_hbc->add_child(replace_all);
  389. replace_all->set_text(TTR("Replace All"));
  390. //replace_all->set_focus_mode(FOCUS_NONE);
  391. replace_all->connect("pressed", this, "_replace_all_pressed");
  392. Control *spacer_split = memnew(Control);
  393. spacer_split->set_custom_minimum_size(Size2(0, 1));
  394. text_vbc->add_child(spacer_split);
  395. VBoxContainer *options_vbc = memnew(VBoxContainer);
  396. add_child(options_vbc);
  397. options_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  398. HBoxContainer *search_options = memnew(HBoxContainer);
  399. options_vbc->add_child(search_options);
  400. case_sensitive = memnew(CheckBox);
  401. search_options->add_child(case_sensitive);
  402. case_sensitive->set_text(TTR("Match Case"));
  403. case_sensitive->set_focus_mode(FOCUS_NONE);
  404. case_sensitive->connect("toggled", this, "_search_options_changed");
  405. whole_words = memnew(CheckBox);
  406. search_options->add_child(whole_words);
  407. whole_words->set_text(TTR("Whole Words"));
  408. whole_words->set_focus_mode(FOCUS_NONE);
  409. whole_words->connect("toggled", this, "_search_options_changed");
  410. error_label = memnew(Label);
  411. search_options->add_child(error_label);
  412. error_label->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
  413. search_options->add_spacer();
  414. hide_button = memnew(TextureButton);
  415. search_options->add_child(hide_button);
  416. hide_button->set_focus_mode(FOCUS_NONE);
  417. hide_button->connect("pressed", this, "_hide_pressed");
  418. replace_options_hbc = memnew(HBoxContainer);
  419. options_vbc->add_child(replace_options_hbc);
  420. replace_options_hbc->hide();
  421. selection_only = memnew(CheckBox);
  422. replace_options_hbc->add_child(selection_only);
  423. selection_only->set_text(TTR("Selection Only"));
  424. selection_only->set_focus_mode(FOCUS_NONE);
  425. selection_only->connect("toggled", this, "_search_options_changed");
  426. }
  427. /*** CODE EDITOR ****/
  428. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  429. Ref<InputEventMouseButton> mb = p_event;
  430. if (mb.is_valid()) {
  431. if (mb->is_pressed() && mb->get_command()) {
  432. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  433. _zoom_in();
  434. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  435. _zoom_out();
  436. }
  437. }
  438. }
  439. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  440. if (magnify_gesture.is_valid()) {
  441. Ref<DynamicFont> font = text_editor->get_font("font");
  442. if (font.is_valid()) {
  443. if (font->get_size() != (int)font_size) {
  444. font_size = font->get_size();
  445. }
  446. font_size *= powf(magnify_gesture->get_factor(), 0.25);
  447. _add_font_size((int)font_size - font->get_size());
  448. }
  449. return;
  450. }
  451. Ref<InputEventKey> k = p_event;
  452. if (k.is_valid()) {
  453. if (k->is_pressed()) {
  454. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  455. _zoom_in();
  456. }
  457. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  458. _zoom_out();
  459. }
  460. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  461. _reset_zoom();
  462. }
  463. }
  464. }
  465. }
  466. void CodeTextEditor::_zoom_in() {
  467. font_resize_val += EDSCALE;
  468. _zoom_changed();
  469. }
  470. void CodeTextEditor::_zoom_out() {
  471. font_resize_val -= EDSCALE;
  472. _zoom_changed();
  473. }
  474. void CodeTextEditor::_zoom_changed() {
  475. if (font_resize_timer->get_time_left() == 0)
  476. font_resize_timer->start();
  477. }
  478. void CodeTextEditor::_reset_zoom() {
  479. Ref<DynamicFont> font = text_editor->get_font("font"); // reset source font size to default
  480. if (font.is_valid()) {
  481. EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
  482. font->set_size(14);
  483. }
  484. }
  485. void CodeTextEditor::_line_col_changed() {
  486. line_nb->set_text(itos(text_editor->cursor_get_line() + 1));
  487. col_nb->set_text(itos(text_editor->cursor_get_column() + 1));
  488. }
  489. void CodeTextEditor::_text_changed() {
  490. if (text_editor->is_insert_text_operation()) {
  491. code_complete_timer->start();
  492. }
  493. idle->start();
  494. }
  495. void CodeTextEditor::_code_complete_timer_timeout() {
  496. if (!is_visible_in_tree())
  497. return;
  498. if (enable_complete_timer)
  499. text_editor->query_code_comple();
  500. }
  501. void CodeTextEditor::_complete_request() {
  502. List<String> entries;
  503. String ctext = text_editor->get_text_for_completion();
  504. _code_complete_script(ctext, &entries);
  505. bool forced = false;
  506. if (code_complete_func) {
  507. code_complete_func(code_complete_ud, ctext, &entries, forced);
  508. }
  509. // print_line("COMPLETE: "+p_request);
  510. if (entries.size() == 0)
  511. return;
  512. Vector<String> strs;
  513. strs.resize(entries.size());
  514. int i = 0;
  515. for (List<String>::Element *E = entries.front(); E; E = E->next()) {
  516. strs[i++] = E->get();
  517. }
  518. text_editor->code_complete(strs, forced);
  519. }
  520. void CodeTextEditor::_font_resize_timeout() {
  521. if (_add_font_size(font_resize_val)) {
  522. font_resize_val = 0;
  523. }
  524. }
  525. bool CodeTextEditor::_add_font_size(int p_delta) {
  526. Ref<DynamicFont> font = text_editor->get_font("font");
  527. if (font.is_valid()) {
  528. int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE);
  529. if (new_size != font->get_size()) {
  530. EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE);
  531. font->set_size(new_size);
  532. }
  533. return true;
  534. } else {
  535. return false;
  536. }
  537. }
  538. void CodeTextEditor::update_editor_settings() {
  539. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  540. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  541. text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  542. text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  543. text_editor->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  544. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  545. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers"));
  546. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_numbers_zero_padded"));
  547. text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_length_guideline"));
  548. text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_length_guideline_column"));
  549. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  550. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  551. text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  552. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  553. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  554. text_editor->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_breakpoint_gutter"));
  555. text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
  556. text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
  557. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  558. text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/open_scripts/smooth_scrolling"));
  559. text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/open_scripts/v_scroll_speed"));
  560. }
  561. void CodeTextEditor::set_error(const String &p_error) {
  562. error->set_text(p_error);
  563. }
  564. void CodeTextEditor::_update_font() {
  565. text_editor->add_font_override("font", get_font("source", "EditorFonts"));
  566. }
  567. void CodeTextEditor::_on_settings_change() {
  568. _update_font();
  569. // AUTO BRACE COMPLETION
  570. text_editor->set_auto_brace_completion(
  571. EDITOR_DEF("text_editor/completion/auto_brace_complete", true));
  572. code_complete_timer->set_wait_time(
  573. EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  574. enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
  575. // call hint settings
  576. text_editor->set_callhint_settings(
  577. EDITOR_DEF("text_editor/completion/put_callhint_tooltip_below_current_line", true),
  578. EDITOR_DEF("text_editor/completion/callhint_tooltip_offset", Vector2()));
  579. }
  580. void CodeTextEditor::_text_changed_idle_timeout() {
  581. _validate_script();
  582. emit_signal("validate_script");
  583. }
  584. void CodeTextEditor::_notification(int p_what) {
  585. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  586. _load_theme_settings();
  587. emit_signal("load_theme_settings");
  588. }
  589. if (p_what == NOTIFICATION_THEME_CHANGED) {
  590. _update_font();
  591. }
  592. }
  593. void CodeTextEditor::_bind_methods() {
  594. ClassDB::bind_method("_text_editor_gui_input", &CodeTextEditor::_text_editor_gui_input);
  595. ClassDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed);
  596. ClassDB::bind_method("_text_changed", &CodeTextEditor::_text_changed);
  597. ClassDB::bind_method("_on_settings_change", &CodeTextEditor::_on_settings_change);
  598. ClassDB::bind_method("_text_changed_idle_timeout", &CodeTextEditor::_text_changed_idle_timeout);
  599. ClassDB::bind_method("_code_complete_timer_timeout", &CodeTextEditor::_code_complete_timer_timeout);
  600. ClassDB::bind_method("_complete_request", &CodeTextEditor::_complete_request);
  601. ClassDB::bind_method("_font_resize_timeout", &CodeTextEditor::_font_resize_timeout);
  602. ADD_SIGNAL(MethodInfo("validate_script"));
  603. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  604. }
  605. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  606. code_complete_func = p_code_complete_func;
  607. code_complete_ud = p_ud;
  608. }
  609. CodeTextEditor::CodeTextEditor() {
  610. code_complete_func = NULL;
  611. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  612. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  613. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  614. find_replace_bar = memnew(FindReplaceBar);
  615. add_child(find_replace_bar);
  616. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  617. find_replace_bar->hide();
  618. text_editor = memnew(TextEdit);
  619. add_child(text_editor);
  620. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  621. find_replace_bar->set_text_edit(text_editor);
  622. text_editor->set_show_line_numbers(true);
  623. text_editor->set_brace_matching(true);
  624. text_editor->set_auto_indent(true);
  625. HBoxContainer *status_bar = memnew(HBoxContainer);
  626. add_child(status_bar);
  627. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  628. idle = memnew(Timer);
  629. add_child(idle);
  630. idle->set_one_shot(true);
  631. idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2));
  632. code_complete_timer = memnew(Timer);
  633. add_child(code_complete_timer);
  634. code_complete_timer->set_one_shot(true);
  635. enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
  636. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  637. error = memnew(Label);
  638. status_bar->add_child(error);
  639. error->set_autowrap(true);
  640. error->set_valign(Label::VALIGN_CENTER);
  641. error->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
  642. error->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  643. error->set_h_size_flags(SIZE_EXPAND_FILL); //required for it to display, given now it's clipping contents, do not touch
  644. status_bar->add_child(memnew(Label)); //to keep the height if the other labels are not visible
  645. Label *line_txt = memnew(Label);
  646. status_bar->add_child(line_txt);
  647. line_txt->set_align(Label::ALIGN_RIGHT);
  648. line_txt->set_valign(Label::VALIGN_CENTER);
  649. line_txt->set_v_size_flags(SIZE_FILL);
  650. line_txt->set_text(TTR("Line:"));
  651. line_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  652. line_nb = memnew(Label);
  653. status_bar->add_child(line_nb);
  654. line_nb->set_valign(Label::VALIGN_CENTER);
  655. line_nb->set_v_size_flags(SIZE_FILL);
  656. line_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
  657. line_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
  658. line_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  659. line_nb->set_align(Label::ALIGN_RIGHT);
  660. line_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  661. Label *col_txt = memnew(Label);
  662. status_bar->add_child(col_txt);
  663. col_txt->set_align(Label::ALIGN_RIGHT);
  664. col_txt->set_valign(Label::VALIGN_CENTER);
  665. col_txt->set_v_size_flags(SIZE_FILL);
  666. col_txt->set_text(TTR("Col:"));
  667. col_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  668. col_nb = memnew(Label);
  669. status_bar->add_child(col_nb);
  670. col_nb->set_valign(Label::VALIGN_CENTER);
  671. col_nb->set_v_size_flags(SIZE_FILL);
  672. col_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
  673. col_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
  674. col_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  675. col_nb->set_align(Label::ALIGN_RIGHT);
  676. col_nb->set("custom_constants/margin_right", 0);
  677. col_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  678. text_editor->connect("gui_input", this, "_text_editor_gui_input");
  679. text_editor->connect("cursor_changed", this, "_line_col_changed");
  680. text_editor->connect("text_changed", this, "_text_changed");
  681. text_editor->connect("request_completion", this, "_complete_request");
  682. Vector<String> cs;
  683. cs.push_back(".");
  684. cs.push_back(",");
  685. cs.push_back("(");
  686. cs.push_back("=");
  687. cs.push_back("$");
  688. text_editor->set_completion(true, cs);
  689. idle->connect("timeout", this, "_text_changed_idle_timeout");
  690. code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");
  691. font_resize_val = 0;
  692. font_size = -1;
  693. font_resize_timer = memnew(Timer);
  694. add_child(font_resize_timer);
  695. font_resize_timer->set_one_shot(true);
  696. font_resize_timer->set_wait_time(0.07);
  697. font_resize_timer->connect("timeout", this, "_font_resize_timeout");
  698. EditorSettings::get_singleton()->connect("settings_changed", this, "_on_settings_change");
  699. }